1

I am getting strange behvaiour I don't know Why? I am adding function at Add listener on button click event but still its is missing as I reload my scene. I have this function. (Its getting desired button object correctly i have tested)

 public void SetupMenuSceneButtons()
    {
        if (GameObject.Find("BtnStartHost"))
            Debug.Log("BtnStartHost found");
        if (GameObject.Find("BtnJoinGame"))
            Debug.Log("BtnJoinGame found");

        GameObject.Find("BtnStartHost").GetComponent<Button>().onClick.RemoveAllListeners();
        GameObject.Find("BtnStartHost").GetComponent<Button>().onClick.AddListener(delegate { StartupHost(); });
        GameObject.Find("BtnJoinGame").GetComponent<Button>().onClick.RemoveAllListeners();
        GameObject.Find("BtnJoinGame").GetComponent<Button>().onClick.AddListener(delegate { JoinGame(); });

    }

I am calling above function on OnLevelWasLoaded event but its function assignment on click is still missing and my buttons are not working as i reload my scene. I have also tried Add event listener without delegate but same problem.

 GameObject.Find("BtnJoinGame").GetComponent<Button>().onClick.AddListener(JoinGame);

I have also tried assignment through this but still problem persist.

GameObject.Find("BtnStartHost").GetComponent<Button>().onClick.RemoveAllListeners();
        GameObject.Find("BtnStartHost").GetComponent<Button>().onClick.AddListener(()=> { StartupHost(); });
        GameObject.Find("BtnJoinGame").GetComponent<Button>().onClick.RemoveAllListeners();
        GameObject.Find("BtnJoinGame").GetComponent<Button>().onClick.AddListener(() => { JoinGame(); });
6
  • Do you actually get the printing indicating the button was found? You could try to invoke right after to see if it registers. Commented Jan 25, 2017 at 7:23
  • @Everts yes i am getting the button object correctly. but how do i invoke onClick through code? Commented Jan 25, 2017 at 7:29
  • GameObject.Find("BtnStartHost").GetComponent<Button>().onClick.Invoke(); Commented Jan 25, 2017 at 7:30
  • @Everts Ok let me check it and inform you Commented Jan 25, 2017 at 7:30
  • @Everts yes invoke working correctly Commented Jan 25, 2017 at 7:32

1 Answer 1

1

I ran a test of your code on v5.5, OnLevelWasLoaded is just not called and triggers a warning for being deprecated (funny it still autocomplete in VS).

The following is working:

 void OnEnable()
 {
     SceneManager.sceneLoaded += SceneManager_sceneLoaded; ;
 }

private void SceneManager_sceneLoaded(Scene arg0, LoadSceneMode arg1)
{
    SetupMenuSceneButtons();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.