Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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


    May 28, 2019
    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


    Jun 28, 2010
    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