- Home /
PauseButton keeps getting null reference exception
void Awake() { myButton = GetComponent(); // <-- you get access to the button component here
myButton.onClick.AddListener(() => { pauseGame(); });
}
Keeps saying null reference exception: object reference is not set to an instance of an object.
Not sure if you just typed in some reference code, but It should be GetComponent();
Ok... That's odd...
GetComponent < Name_Of_Component > ();
Answer by Lairinus · Oct 15, 2017 at 05:15 PM
In reference to this code posted by @blchastain: http://answers.unity3d.com/storage/attachments/103802-pause-game.txt
Null Reference Exception on Button object:
Make sure that you have a "Button" component attached to the GameObject where this script is attached
Try the following code:
void Awake() { myButton = gameObject.GetComponent<Button>(); // <-- you get access to the button component here myButton.onClick.AddListener(() => pauseGame()); // <-- you assign a method to the button OnClick event here }
The code above is more explicit, but may not make any difference.
Also, as a side note:
"void Start()" is the MonoBehaviour method; "void start" is what you are currently using.