Search Unity

Instantiate Text not visible in scene

Discussion in 'UGUI & TextMesh Pro' started by Magnumstar, Nov 22, 2015.

  1. Magnumstar

    Magnumstar

    Joined:
    Oct 3, 2015
    Posts:
    304
    I have a 2D game with a UI Text prefab I made, dragged into inspector on my public Text field in GameManager. In GameManager i instantiate this text and set the text property. I cannot see the text when I play scene. This has been giving me issues for the better half of the day, and I can't seem to find out why. I even tried to set parent as canvas, still nothing. What am I doing wrong?
     
  2. FeatureCreeep

    FeatureCreeep

    Joined:
    Nov 22, 2015
    Posts:
    2
    I'm pretty new at this too but I had a similar struggle. Here are some thoughts.

    You definitely need to set the parent. Make sure to use "transform.SetParent(parentName, false);" and not "transform.parent = ".

    You also need to make sure you are setting the position of that Text. I tried to set the position of my prefab after I instantiated my it but I was more successful just setting it in the Instantiate call itself using the overload that takes a Vector3.

    Also try just dragging the prefab unto your canvas to make sure it displays correctly. I've had issues where other problems with the prefab were preventing it from being seen and the Instantiate was working fine.

    If none of those work, try posting the code and maybe we can figure it out.

    Good luck.
     
  3. Magnumstar

    Magnumstar

    Joined:
    Oct 3, 2015
    Posts:
    304
    I did all of these things and none of that worked. Heres my code

    Code (csharp):
    1.  
    2.         //Display Points
    3.         _canvas = GameObject.FindGameObjectWithTag ("Canvas").GetComponent<Canvas>();
    4.         _text = Instantiate (_textPrefab, new Vector3 (0, 0, 0f), Quaternion.identity) as Text;
    5.         _text.text = _points.ToString ();
    6.         _text.transform.SetParent (_canvas.transform);
    7.  
    Edit** This seems to be relevant. When I start the scene and select the Text(Clone) from the Hierarchy, the Canvas becomes selected and I don't see any text. However, the text property within the Text(Clone) object is my "0" points it should be as programmed.
     
  4. Magnumstar

    Magnumstar

    Joined:
    Oct 3, 2015
    Posts:
    304
    I left the game for an hour came back and played the scene and the text is there in the middle, hrmm. Thanks.
     
  5. codeguyross1

    codeguyross1

    Joined:
    Nov 26, 2012
    Posts:
    3
    This worked for me but I had to set to the parent transform and not the parent name

    "transform.SetParent(parent.transform, false);"