Search Unity

Unity UI How can I instantiate gameObject as UI.Text in unity

Discussion in 'UGUI & TextMesh Pro' started by Teymur12, Nov 7, 2020.

  1. Teymur12

    Teymur12

    Joined:
    May 28, 2019
    Posts:
    1
    Hello, I'm working on my game. I'm trying to instantiate text and then change instantiated text's text value. Like this:

    But I'm getting an error like this, and I cannot find the way to solve it.

    numbersfortune and objectsfortune are structs. Can you help please? Thanks. Aghayev Teymur.
     
  2. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,913
    That might only work if the SimpleText is in a field like
    public Text SimpleText;
    . It can automatically instantiate the GameObject then get the component for you. It can't if it looks like
    public GameObject SimpleText;
    .

    You can change the field to text, or instantiate as GameObject, then do GetComponent<Text>().
     
    Last edited: Nov 7, 2020