Instantiated Text not visible.

Hi everybody, I’m beginner in Unity. I’m trying to write a script (c#) that create a list of Text (for a highscore board), but the instantiated Text are not visible. Here is my script :

public Text m_LeaderBoardName;

[...]

List<ScoreEntry> scores = m_LeaderBoard.GetHighScores();
       
        for(int i = 0; i < scores.Count; i++)
        {
            Vector3 offset = new Vector3(0, i * 20, 0);

            Text LBName = (Text)Instantiate(m_LeaderBoardName, m_LeaderBoardName.transform.position, Quaternion.identity);
            LBName.transform.position += offset;
            LBName.text = scores[i].m_Name;
       }

where m_LeaderBoardName is a Text on my gameobject hierarchy. The Texts are effectively instantiated, with pertinent position and text content, but not visible. What am I do wrong ? Feel free to asks me for any precision. Thanks.

you need to parent the instantiated text to something in a hierarchy starting with a canvas

check out the last line in PopulateList function of the CreateScrollList in the example:

1 Like

I am not sure if you can instantiate a MonoBehaviour (like Text).
I always instantiate GameObjects and Get the components i need from them.

you can instantiate like that. The gameobject will be created (as it would had you just used a game object) but the returned component will be of the type you have specified.

I.e. if you have a prefab game object with a script and a rigidbody attached you can instantiate the “rigidbody” to get the entire prefab created in the scene (go, script and rb) and a returned reference to the rigidbody component

there are examples of this in the scripting reference: Unity - Scripting API: Object.Instantiate

I set the parent, all is working now, thanks everybody :slight_smile: