- Home /
Change the text of a UI button on clicking it?
When i click my ui button it changes the text once. then my script wont change the text back when clicked again. Ive tried about everything i can think of so asking for help here. guessing im overtired and missing it. the debug log only prints "1", one time, every time i click the button.
public void HealthBars()
{
if (PlayerPrefs.GetInt("HealthBars") == 1)
{
PlayerPrefs.SetInt("Healthbars", 0);
HBButton.text = "Turn On Enemy Healthbars Test";
Debug.Log("0");
}
else
{
PlayerPrefs.SetInt("Healthbars", 1);
HBButton.text = "Turn Off Enemy Healthbars Test";
Debug.Log("1");
}
}
Answer by Ran-Quan · Mar 21, 2017 at 06:07 AM
PlayerPrefs keys are case-sensitive, so PlayerPrefs.GetInt("HealthBars")
will always return 0 since you are setting "Healthbars" instead of "HealthBars".
thank you!!! lol i knew it was a tired error i wasn't seeing :)
Your answer
Follow this Question
Related Questions
How to detect button presses and change child object text in the button's Parent Object script 0 Answers
PlayerPrefs, it doesn't change the text after restarting the game 0 Answers
Write in button 2 Answers
I need to resize my text to button to be visible using Unity 4.6 UI 2 Answers
How To Change Color Of Text On UI When It's Selected | Unity 4.6 3 Answers