Answer the question
In order to leave comments, you need to log in
NullReferenceException: Object reference not set to an instance of an object error in Unity?
Good afternoon, as soon as I start my project, I get the following error NullReferenceException: Object reference not set to an instance of an object
selfquest.OnTriggerEnter2D, I don’t understand why, so I ask for your help.
Here is the code that appears when I click on the error:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class самквест : MonoBehaviour
{
public int qestnumber;
public int[] items;
public GameObject[] clouds;
public GameObject barer;
public GameObject key;
public void OnTriggerEnter2D(Collider2D other)
{
if (other.tag != "Player" && other.gameObject.GetComponent<sobiraenpredmetu>().id == items[qestnumber])//ругается на эту строчку
{
qestnumber++;
Destroy(other.gameObject);
CheckQuest();
}
}
public void CheckQuest()
{
for (int i = 0; i < clouds.Length; i++)
{
if (i == qestnumber)
{
clouds[i].SetActive(true);
clouds[i].GetComponent<Animator>().SetTrigger("istrigger");
break;
}
else
{
clouds[i].SetActive(false);
}
}
if (qestnumber == 2)
{
barer.SetActive(false);
}
if (qestnumber == 3)
{
key.SetActive(true);
}
}
}
Answer the question
In order to leave comments, you need to log in
1) The object that triggered does not have this component.
2) Your index goes beyond the array, i.e. index is greater than the number of elements in the array. In the inspector, enter a number, check the ID given in the condition, or check the presence of elements in the array. You most likely have a problem there.
Hope it helped. And it is correct to write not "qest", but "quest" :)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question