Search Unity

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

Discussion in 'UGUI & TextMesh Pro' started by AtticusMarkane, Sep 4, 2014.

  1. AtticusMarkane

    AtticusMarkane

    Joined:
    Aug 6, 2014
    Posts:
    18
    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:
    LoadingUI.JPG
    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:
    manual.JPG

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

    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.
     
  2. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,225
    How are you setting the UI to be a child of the canvas? In the FAQ we detail the correct way to do this.
     
  3. AtticusMarkane

    AtticusMarkane

    Joined:
    Aug 6, 2014
    Posts:
    18
    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?
     
  4. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,225
    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).
     
  5. AtticusMarkane

    AtticusMarkane

    Joined:
    Aug 6, 2014
    Posts:
    18
    Code (CSharp):
    1. GameObject child = (GameObject)GameObject.Instantiate(source);
    2.  
    3.             child.name = !string.IsNullOrEmpty(name) ? name : source.name;
    4.  
    5.             Transform childTransform = child.transform;
    6.             childTransform.parent = parent;
    I have updated it to childTransform.SetParent(canvas, false) and it seems to be working perfectly. Thanks for the help.
     
    bz_tian and Tim-C like this.