Instantiating Panel and adding it to the Canvas results in it being "invisible"

I have a scene with an Overlay canvas and a game object for my loading screen in my resources folder, the canvas has all of it’s default settings and here is the game object:


You will notice that the LoadingUI object just contains a Rect Transform and my custom script and acts as a container for the rest of the screen’s content.

If I drag the LoadingUI object into the hierarchy as a child of the canvas, everything works as expected:

The issue comes when I instead run the project and instantiate the prefab at runtime:

Has anyone run into a similar issue before and if so how did you fix it? I’ve been trying everything I could think of but to no avail.

How are you setting the UI to be a child of the canvas? In the FAQ we detail the correct way to do this.

Likely the incorrect way as I did not come across this. Simply transform.setParent, could you help direct me to the FAQ for this problem?

That’s what the FAQ says transform.SetParent (newParent, false);

what is the code you are using for the spawning / parenting (as in paste the code here).

GameObject child = (GameObject)GameObject.Instantiate(source);

            child.name = !string.IsNullOrEmpty(name) ? name : source.name;

            Transform childTransform = child.transform;
            childTransform.parent = parent;

I have updated it to childTransform.SetParent(canvas, false) and it seems to be working perfectly. Thanks for the help.

2 Likes