Answer the question
In order to leave comments, you need to log in
Error in Unity3D NullReferenceException: Object reference not set to an instance of an object. Why?
I'm making a simple game. When a player collects boxes, the text object is updated, which shows the number of collected boxes. But I get an error NullReferenceException: Object reference not set to an instance of an object and the text is not updated.
Please help, I've already spent half a day and I can't find the error.
CounterController class
public class CounterController : MonoBehaviour {
int numberOfBoxes;
Text counterView;
// Use this for initialization
void Start () {
ResetCounter ();
}
public void IncrementCounter(){
numberOfBoxes++;
counterView.text = numberOfBoxes.ToString();
}
public void ResetCounter(){
numberOfBoxes=0;
counterView.text = numberOfBoxes.ToString();
}
}
public class PickUpBox : MonoBehaviour {
CounterController counterController;
void Start ()
{
counterController = GameObject.Find ("Manager").GetComponent<CounterController> ();
if (counterController == null)
{
Debug.LogError ("CounterController не найден.");
}
}
void OnTriggerEnter2D (Collider2D other)
{
if (other.gameObject.name == "Girl")
{
Destroy (this.gameObject);
counterController.IncrementCounter ();
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question