D
D
Daniel23102021-06-29 13:36:56
Unity
Daniel2310, 2021-06-29 13:36:56

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);
        }
    }
}

I will be very grateful to you if you help me. And one more thing: as soon as I throw the object I need on the stage and run it, I get a current error - IndexOutOfRangeException: Index was outside the bounds of the array.
selfquest.OnTriggerEnter2D(UnityEngine.Collider2D other. I would be very grateful if you could help me with this error as well.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
Hixac, 2021-06-29
@Hixac

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 question

Ask a Question

731 491 924 answers to any question