NullReferenceException: Object reference not set to an instance of an object

Hi guys, I’m working on a project. On this project I add a button script for canvas, when its clicked it search which object (in my case question-3dcube) picked. But, Im getting this error after I press the button… I need a quick help please!!

Here is the error:
NullReferenceException: Object reference not set to an instance of an object
QuestionBtns.WhichQuestion () (at Assets/Script/QuestionBtns.cs:273)
QuestionBtns.FalsePressed () (at Assets/Script/QuestionBtns.cs:182)
UnityEngine.Events.InvokableCall.Invoke (System.Object[ ] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:144)
UnityEngine.Events.InvokableCallList.Invoke (System.Object[ ] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:621)
UnityEngine.Events.UnityEventBase.Invoke (System.Object[ ] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:756)
UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:53)
UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44)
UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52)
UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269)
UnityEngine.EventSystems.EventSystem:Update()

Here is the Code:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class QuestionBtns : MonoBehaviour {
    public Text Qtext;
    public GameObject LDoor1;
    public GameObject LDoor2;
    public GameObject LDoor3;
    public GameObject LDoor4;
    public GameObject LDoor5;
    public GameObject LDoor6;
    public GameObject LDoor7;
    public GameObject LDoor8;
    public GameObject LDoor9;
    public GameObject LDoor10;
    public GameObject LDoor11;
    public GameObject LDoor12;
    public GameObject LDoor13;
    public GameObject LDoor14;
    public GameObject LDoor15;
    public GameObject LDoor16;
    public GameObject LDoor17;
    public GameObject RDoor1;
    public GameObject RDoor2;
    public GameObject RDoor3;
    public GameObject RDoor4;
    public GameObject RDoor5;
    public GameObject RDoor6;
    public GameObject RDoor7;
    public GameObject RDoor8;
    public GameObject RDoor9;
    public GameObject RDoor10;
    public GameObject RDoor11;
    public GameObject RDoor12;
    public GameObject RDoor13;
    public GameObject RDoor14;
    public GameObject RDoor15;
    public GameObject RDoor16;
    public GameObject RDoor17;
    public GameObject BDoor1;
    public GameObject BDoor2;
    public GameObject BDoor3;
    public GameObject BDoor4;
    public GameObject BDoor5;
    public GameObject BDoor6;
    public GameObject BDoor7;
    public GameObject BDoor8;
    public GameObject BDoor9;
    public GameObject BDoor10;
    public GameObject BDoor11;
    public GameObject BDoor12;
    public GameObject BDoor13;
    public GameObject BDoor14;
    public GameObject BDoor15;
    public GameObject BDoor16;
    public GameObject BDoor17;
    public GameObject Question1;
    public GameObject Question2;
    public GameObject Question3;
    public GameObject Question4;
    public GameObject Question5;
    public GameObject Question6;
    public GameObject Question7;
    public GameObject Question8;
    public GameObject Question9;
    public GameObject Question10;
    public GameObject Question11;
    public GameObject Question12;
    public GameObject Question13;
    public GameObject Question14;
    public GameObject Question15;
    public GameObject Question16;
    public GameObject Question17;

    public GameObject[]Questions = new GameObject[20];
    public int count;

    // Use this for initialization
    void Start () {
        Qtext = Qtext.GetComponent<Text>();
        LDoor1 = GameObject.Find("LCube (1)");
        LDoor2 = GameObject.Find("LCube (2)");
        LDoor3 = GameObject.Find("LCube (3)");
        LDoor4 = GameObject.Find("LCube (4)");
        LDoor5 = GameObject.Find("LCube (5)");
        LDoor6 = GameObject.Find("LCube (6)");
        LDoor7 = GameObject.Find("LCube (7)");
        LDoor8 = GameObject.Find("LCube (8)");
        LDoor9 = GameObject.Find("LCube (9)");
        LDoor10 = GameObject.Find("LCube (10)");
        LDoor11 = GameObject.Find("LCube (11)");
        LDoor12 = GameObject.Find("LCube (12)");
        LDoor13 = GameObject.Find("LCube (13)");
        LDoor14 = GameObject.Find("LCube (14)");
        LDoor15 = GameObject.Find("LCube (15)");
        LDoor16 = GameObject.Find("LCube (16)");
        LDoor17 = GameObject.Find("LCube (17)");

        RDoor1 = GameObject.Find("RCube (1)");
        RDoor2 = GameObject.Find("RCube (2)");
        RDoor3 = GameObject.Find("RCube (3)");
        RDoor4 = GameObject.Find("RCube (4)");
        RDoor5 = GameObject.Find("RCube (5)");
        RDoor6 = GameObject.Find("RCube (6)");
        RDoor7 = GameObject.Find("RCube (7)");
        RDoor8 = GameObject.Find("RCube (8)");
        RDoor9 = GameObject.Find("RCube (9)");
        RDoor10 = GameObject.Find("RCube (10)");
        RDoor11 = GameObject.Find("RCube (11)");
        RDoor12 = GameObject.Find("RCube (12)");
        RDoor13 = GameObject.Find("RCube (13)");
        RDoor14 = GameObject.Find("RCube (14)");
        RDoor15 = GameObject.Find("RCube (15)");
        RDoor16 = GameObject.Find("RCube (16)");
        RDoor17 = GameObject.Find("RCube (17)");

        BDoor1 = GameObject.Find("BCube (1)");
        BDoor2 = GameObject.Find("BCube (2)");
        BDoor3 = GameObject.Find("BCube (3)");
        BDoor4 = GameObject.Find("BCube (4)");
        BDoor5 = GameObject.Find("BCube (5)");
        BDoor6 = GameObject.Find("BCube (6)");
        BDoor7 = GameObject.Find("BCube (7)");
        BDoor8 = GameObject.Find("BCube (8)");
        BDoor9 = GameObject.Find("BCube (9)");
        BDoor10 = GameObject.Find("BCube (10)");
        BDoor11 = GameObject.Find("BCube (11)");
        BDoor12 = GameObject.Find("BCube (12)");
        BDoor13 = GameObject.Find("BCube (13)");
        BDoor14 = GameObject.Find("BCube (14)");
        BDoor15 = GameObject.Find("BCube (15)");
        BDoor16 = GameObject.Find("BCube (16)");
        BDoor17 = GameObject.Find("BCube (17)");

        Question1 = GameObject.Find("CQuestionBar1");
        Question2 = GameObject.Find("CQuestionBar1 (2)");
        Question3 = GameObject.Find("CQuestionBar1 (7)");
        Question4 = GameObject.Find("CQuestionBar1 (9)");
        Question5 = GameObject.Find("CQuestionBar1 (3)");
        Question6 = GameObject.Find("CQuestionBar2 (3)");
        Question7 = GameObject.Find("CQuestionBar1 (4)");
        Question8 = GameObject.Find("WQuestionBar2 (4)");
        Question9 = GameObject.Find("WQuestionBar2 (1)");
        Question10 = GameObject.Find("WQuestionBar2");
        Question11 = GameObject.Find("WQuestionBar2 (6)");
        Question12 = GameObject.Find("WQuestionBar1 (1)");
        Question13 = GameObject.Find("WQuestionBar2 (7)");
        Question14 = GameObject.Find("WQuestionBar2 (8)");
        Question15 = GameObject.Find("WQuestionBar1 (6)");
        Question16 = GameObject.Find("WQuestionBar1 (5)");
        Question17 = GameObject.Find("WQuestionBar2 (5)");

        Questions[0] = Question1;
        Questions[1] = Question2;
        Questions[2] = Question3;
        Questions[3] = Question4;
        Questions[4] = Question5;
        Questions[5] = Question6;
        Questions[6] = Question7;
        Questions[7] = Question8;
        Questions[8] = Question9;
        Questions[9] = Question10;
        Questions[10] = Question11;
        Questions[11] = Question12;
        Questions[12] = Question13;
        Questions[13] = Question14;
        Questions[14] = Question15;
        Questions[15] = Question16;
        Questions[16] = Question17;
        count = 17;


    }
   
    // Update is called once per frame
    void Update () {
   
    }
    public void FalsePressed()
    {
       int number = WhichQuestion();
        if(number == -1)
        {
            Qtext.text = "Something went wrong";
        }
        else {
            if(number == 0)
            {
                RDoor1.SetActive(true);
                BDoor1.SetActive(true);
            }else if(number == 1)
            {
                RDoor10.SetActive(true);
                BDoor3.SetActive(true);
            }else if(number == 2)
            {
                RDoor13.SetActive(true);
                BDoor6.SetActive(true);
            }else if(number == 3)
            {
                RDoor11.SetActive(true);
                BDoor8.SetActive(true);
            }else if(number == 4)
            {
                RDoor14.SetActive(true);
                BDoor5.SetActive(true);
            }else if(number == 5)
            {
                RDoor7.SetActive(true);
                BDoor15.SetActive(true);
            }else if(number == 6)
            {
                RDoor15.SetActive(true);
                BDoor13.SetActive(true);
            }else if(number == 7)
            {
                RDoor6.SetActive(true);
                BDoor16.SetActive(true);
            }else if(number == 8)
            {
                RDoor8.SetActive(true);
                BDoor14.SetActive(true);
            }else if(number == 9)
            {
                RDoor17.SetActive(true);
                BDoor9.SetActive(true);
            }else if(number == 10)
            {
                RDoor2.SetActive(true);
                BDoor10.SetActive(true);
            }else if(number == 11)
            {
                RDoor9.SetActive(true);
                BDoor2.SetActive(true);
            }else if(number == 12)
            {
                RDoor16.SetActive(true);
                BDoor2.SetActive(true);
            }else if(number == 13)
            {
                RDoor3.SetActive(true);
                BDoor11.SetActive(true);
            }else if(number == 14)
            {
                RDoor12.SetActive(true);
                BDoor7.SetActive(true);
            }else if(number == 15)
            {
                RDoor4.SetActive(true);
                BDoor4.SetActive(true);
            }else if(number == 16)
            {
                RDoor5.SetActive(true);
                BDoor17.SetActive(true);
            }
        Qtext.text = "Then choose the left path!";
        Time.timeScale = 1;
        }
    }
    public void TruePressed()
    {
        Qtext.text = "Then choose the right path!";
       
        Time.timeScale = 1;
    }
    public int WhichQuestion()
    {
        int num = -1;

        for(int i = 0; i < count; i++)
        {
            if(Questions[i].gameObject.activeInHierarchy == false)
            {
                num = i;
                break;
            }
        }
        return num;
    }
}

would love to get quick responses!!

I’m assuming all your public GameObjects are there to hold prefabs? Are all your prefabs in place?

The easiest way to figure out what is going on is to drop a few Debug.Log statements in your WhichQuestion() For loop. Log out the i value so you can figure out which one it is bombing on. The error in the stack trace indicates that one of the items in the array are not instantiated. First check your prefabs, then debug each iteration.

Also, instead of using count in your loops (and other places I see), use Questions.Length instead. Much cleaner and less chance of making mistakes.

Hope this helps.

Why are you using 68 hardcoded fields instead of just 4 lists? Also, why are they public when you initialize them in Start()?

it’s slightly worse than that… he’s centrally controlling 17 independent objects (with 2 parts each) via 68 hardcoded variables instead of building a simpler “question links to lock for specific set of doors” approach.

I have a problem please help
NullReferenceException: Object reference not set to an instance of an object
PlayerShip_script.BtnFire () (at Assets / SS_Scripts / PlayerShip_script.cs: 134)
UnityEngine.Events.InvokableCall.Invoke () (at C: /buildslave/unity/build/Runtime/Export/UnityEvent.cs: 165)
UnityEngine.Events.UnityEvent.Invoke () (at C: /buildslave/unity/build/Runtime/Export/UnityEvent_0.cs: 58)
UnityEngine.UI.Button.Press ()


When you want to post code on the forums, it’s better to post it directly and not a screenshot: Using code tags properly

This error is general, and you could create a new thread for your issue (in the future, please).

It looks as though you never assign your animator variable “MyAnimator”.
You can make the variable public or use the attribute “SerializeField” and assign it in the inspector.
If it’s on the same game object, you could also use GetComponent in Awake().

Hope that helps. :slight_smile: