ui.text string matching doesn't work
i have a C# source code which looks like this:
public Text txtState;
void Start () {
txtState = txtState.GetComponent <Text> ();
txtState.text = "1";
}
void Update () {
if (string.Equals(txtState.text,"1")) {
btnA.onClick.AddListener (delegate {txtState.text = "2"; Debug.Log("going state 2");});
}else if (string.Equals(txtState.text,"2")){
btnB.onClick.AddListener (delegate {txtState.text = "3"; Debug.Log("going to state 3");});
}//else if statement goes on
}
when i try to run it and click on btnA
, and then clicked it the second time, the statement inside the first condition statement is still accepted (when it should not) even after changing the value txtState.text
to 2
. Please help, i'm a beginner in using C# and Unity so i'm clueless on what i'm doing wrong ):
Do you realize that if the text equals to "1" or "2", then you will add listeners each frames since you are in the Update loop ?
Note that the condition you have above the add listener command is NOT included in the listener. So, if you DON'T want it to call the listener that sets the text =2, the next time the button is clicked, you will need to RE$$anonymous$$OVE that listener. http://docs.unity3d.com/462/Documentation/ScriptReference/Events.UnityEvent.RemoveListener.html
edit: possible Alternative- you can define the listeners to INCLUDE the if statements, which would also eli$$anonymous$$ate the any need to CHANGE listeners. This way, it can be assigned once at start(), and you don't need to worry about adding or removing them after that.