I have a C# script called MainMenu.cs
Here it is in its entirety
using UnityEngine;
using UnityEngine.UI;
public class MainMenu : MonoBehaviour
{
public Button btnPlay;
// Use this for initialization
void Start ()
{
btnPlay = btnPlay.GetComponent<Button>();
}
public void PlayLevel()
{
Application.LoadLevel(1);
}
}
In my hierarchy, i have a Main Camera, a Canvas and EventSystem. Under Canvas i have a Button. I add the MainMenu script to the Canvas and associate the button with Button i have in canvas. When i go to the button and associate the MainMenu with its OnClick I am expecting to be able to chose PlayLevel() but it is not there.
Am i missing something obvious here? (Using Unity 5.0)
btnPlay = btnPlay.GetComponent<Button>();
Uh. I don't think you need this. EitherbtnPlay
is already assigned (in which case this code is a NOP) or it isn't (in which case this code will Null Pointer). \$\endgroup\$