Change Button Text ?

Hi,

i have a script on my camera which moves the camera to a specific position.
when i klick a button in my game i want to start the movement and change the button text from “move in” to “move out” then the camera ride is over.

I tried to change the text from within the camera script but i just cant access the button text.
In general, is it a good idea to access the button text from within the camera or are there better ways?
How can i change the button text after something happened and change it to something different after clicking it again?

Ah, after trying so much its that easy…

GameObject.Find("buttonName").GetComponentInChildren<Text>().text = "la di da";
25 Likes

Thanks.

And if you run into “The type or namespace name ‘Text’ could not be found” - add

using UnityEngine.UI;

(found in unity 4.6 UI "Text" could not bet found? )

4 Likes

thanks @dichterDichter it works well
but i also want to change the color and font of text can anyone help me out

bless you for this. Spent a 1/2 hour searching, searching, searching

Dude, how do you know what to put?(buttonName)

the name of the button in your scene if you didnt change the name its called Button

thankuuuu u literally saves me

1 Like

cant we do it from unity itself???

I was able to change the button text in Unity by clicking the down arrow next to “Button” in the Hierarchy menu on the lefthand side of my screen. Once I clicked the down arrow the word “Text” appeared and on the right hand side of my screen in the Inspector. I was then able to edit the text/font/size/color. I hope this helps!

7 Likes

This is what i have been looking for THANK YOU!

Yes, we can do that from the engine. However, the poster was asking how to change it in a script.

:slight_smile: thank you!