[4.6 UI] What method do I need to call from the gameObject to modify a Text UI?

I’ve declared this at the top of the script

  • publicGameObject scoreText;

In the editor i’ve dragged the Text UI element into this gameobject slot i created (im sorry i am fairly new and don’t know what these boxes would be referenced as)

So now that i have the Text UI element as a game object, i want to modify the text in the script.

I have a function to keep score

public void LZActivated()
     {
         levelScore += 500;
        
         NumLZActivated++;
         if(NumLZActivated == TotalLZ)
         {
             Win();
         }
     }

But im looking for something like this that i can the test field to

public void LZActivated()
     {
         levelScore += 500;
         scoreText.Text = "SCORE - " + levelScore;
         NumLZActivated++;
         if (NumLZActivated == TotalLZ)
         {
             Win();
         }
     }

Questions , please ask. I hope i worded my questions and goal well. Thank you in advanced for your help
-Jon

scoreText.GetComponent<Text>().text = "SCORE - " + levelScore;"

If you call it often / regularly you should probably cache the Text component into variable.

1 Like