The question is answered, right answer was accepted
Text on UI Button Not Updating
Hi there,
I have a button that's suppose to toggle between a map editor and the game state. The button correctly toggles the game state, but it does not correctly update the text on the button. Yet if I debug the button text...it shows that it should have changed. I have absolutely no idea what to do here. Code:
[SerializeField] Button mapEditorButton = default;
string mapEditorButtonLabel;
private void Start()
{
mapEditorButtonLabel = mapEditorButton.GetComponentInChildren<Text>().text;
}
public void ToggleMapEditor() {
bool mapEditorActive = Game.Instance.GetInputState().GetType() == typeof(InputEditorDefault);
if (mapEditorActive) {
mapEditorButtonLabel = "Open Map Editor";
Game.Instance.PopInputState();
}
else {
mapEditorButtonLabel = "Return to Game";
Game.Instance.LaunchMapEditor();
}
Debug.Log(mapEditorButtonLabel);
Debug.Log(Game.Instance.GetInputState().GetType());
}
The button eternally shows the placeholder text I have for it, despite the following output:
I am using 2019.2.6f1 personal :)
Answer by grahamegw · Mar 20, 2020 at 05:54 PM
Figured it out. Apparently you cannot cache properties as variables, so having mapEditorButtonLabel
be a string and assigning Button.Text.text
to it is invalid. Changed it to Text = mapEditorButtonLabel
, and assigned mapEditorButton.GetComponentInChildren<Text>()
to it, and then did the assignment through mapEditorButtonLabel.text = foo
Follow this Question
Related Questions
Smooth UI sprite animation. 1 Answer
What have I done wrong on the OnClick script from Creating a Main Menu tutorial? 0 Answers
How do I press an UI button with Gamepad controls? 0 Answers
Slide-Out Panel Animates on Button - How to Animate on Swipe Motion on Mobile? 1 Answer
is it possible to have two on click events in one button 1 Answer