I’ve been trying to change the Onclick event I set up in the editor of a button to something else.I’m Calling removeAllListeners() as some examples I’ve found and then adding as a listener an anonymous function. When I click the button after OnGameEnd Runs, I still see the wrong event ocurring and no loadLevel. I now OnGameEnd does run since I can see the text changing. Any ideas of what I’m doing wrong or is this a bug? For now I’ll work around this just checking a boolean in the event I set in the editor, but I would like to know how to change button events in runtime properly.
In the code below, “arrowButton” is same as GetComponent()
, I just cached it in a private variable at the start.
/// <summary>
/// Re-enables arrow, makes it so we load the next level once we click it.
/// </summary>
public void OnGameEnd() {
arrowButton.interactable = true;
arrowAnimator.SetBool("Disabled", false);
GetComponentInChildren<Text>().text = gameEndText;
// When this arrow is clicked now that the game is over, it'll just load the next level.
arrowButton.onClick.RemoveAllListeners();
arrowButton.onClick.AddListener(() => Application.LoadLevel(nextLevelName));
}