- Home /
Question by
luken · Oct 05, 2016 at 05:26 AM ·
button trigger events
NullReferenceException: Object reference not set to an instance of an object
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class Firestation_answer : MonoBehaviour {
public Button fireA;
void Start () {
Button btn = fireA.GetComponent<Button>();
if (btn) {
btn.onClick.AddListener (TaskOnClick);
} else {
Debug.Log ("Nothing was clicked ! ");
}
}
void TaskOnClick(){
Debug.Log ("You have clicked the button!");
}
}
_hello goodmorning, I'm super new in Unity3d and this simple code returns an error " NullReferenceException: Object reference not set to an instance of an object Firestation_answer.Start () (at Assets/Scripts/Firestation_answer.cs:12)" .why ? what's wrong with the code ? .thanks in advance for the help :) .I just want to print if the certain button is clicked or not.
Comment
Did you set the fireA in the inspector? fireA is already a button so there's no reason to get the button component from it again and save it in another variable.