I have a prefab I instantiate during play, that has a child canvas rendered in world space. I need this child canvas to display some floating UI elements specific to each instance of the prefab.
However I don't know how to access the child canvas. I usually access child objects using GetChild, but it doesn't seem to be working for the canvas (it throws a transform out of bounds error).
CodePudding user response:
Create a new script in the top most gameobject of the prefab.
public Transform Canvas;
Drop the canvas onto it. Then when you instantiate the prefab you can do this:
var obj = Instantiate(prefab, position, rotation) as Transform;
var canvas = obj.GetComponent<MyScript>().Canvas;
And that's it. Easy access from a top level script.