Question by
comolokko11 · May 26, 2020 at 09:04 PM ·
uibuttonmenu
Reset Text
pressing the button on this code changes the text on the button, but when I press the button again, the text does not return to its original text. I want to go back to the old text when I press it again.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ButtonHandler : MonoBehaviour {
public void SetText(string text)
{
Text txt = transform.Find("Text").GetComponent<Text>();
txt.text = text;
}
}
Comment
Answer by twosuliman · May 27, 2020 at 10:14 AM
Try doing something like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ButtonHandler : MonoBehaviour {
string oldText = transform.Find("Text").GetComponent<Text>().text;
public void SetText(string text)
{
Text txt = transform.Find("Text").GetComponent<Text>();
if(text != oldText){
oldText = txt.text;
txt.text = text;
}
else
{
txt.text = oldText;
}
}
Assets\Scripts\ButtonHandler.cs(22,8): error CS1513: } expected