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?
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!
public class SetButtonText : MonoBehaviour
{
[SerializeField] private TextMeshProUGUI buttonText;
void Start()
{
if (buttonText == null)
{
// Try automatically getting the TMP text component from children
buttonText = GetComponentInChildren<TextMeshProUGUI>();
}
if (buttonText != null)
{
buttonText.text = "What you want the text to be";
}
else
{
Debug.LogWarning("No TextMeshProUGUI component found on this button.");
}
}
}
And once its on the button, scroll down to the script, And for Button Text, set it to 'Text (TMP) and then its all good