Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'Scripting' started by Mr_Botz, May 19, 2016.

  1. Mr_Botz

    Mr_Botz

    Joined:
    Nov 5, 2015
    Posts:
    2
    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:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class QuestionBtns : MonoBehaviour {
    6.     public Text Qtext;
    7.     public GameObject LDoor1;
    8.     public GameObject LDoor2;
    9.     public GameObject LDoor3;
    10.     public GameObject LDoor4;
    11.     public GameObject LDoor5;
    12.     public GameObject LDoor6;
    13.     public GameObject LDoor7;
    14.     public GameObject LDoor8;
    15.     public GameObject LDoor9;
    16.     public GameObject LDoor10;
    17.     public GameObject LDoor11;
    18.     public GameObject LDoor12;
    19.     public GameObject LDoor13;
    20.     public GameObject LDoor14;
    21.     public GameObject LDoor15;
    22.     public GameObject LDoor16;
    23.     public GameObject LDoor17;
    24.     public GameObject RDoor1;
    25.     public GameObject RDoor2;
    26.     public GameObject RDoor3;
    27.     public GameObject RDoor4;
    28.     public GameObject RDoor5;
    29.     public GameObject RDoor6;
    30.     public GameObject RDoor7;
    31.     public GameObject RDoor8;
    32.     public GameObject RDoor9;
    33.     public GameObject RDoor10;
    34.     public GameObject RDoor11;
    35.     public GameObject RDoor12;
    36.     public GameObject RDoor13;
    37.     public GameObject RDoor14;
    38.     public GameObject RDoor15;
    39.     public GameObject RDoor16;
    40.     public GameObject RDoor17;
    41.     public GameObject BDoor1;
    42.     public GameObject BDoor2;
    43.     public GameObject BDoor3;
    44.     public GameObject BDoor4;
    45.     public GameObject BDoor5;
    46.     public GameObject BDoor6;
    47.     public GameObject BDoor7;
    48.     public GameObject BDoor8;
    49.     public GameObject BDoor9;
    50.     public GameObject BDoor10;
    51.     public GameObject BDoor11;
    52.     public GameObject BDoor12;
    53.     public GameObject BDoor13;
    54.     public GameObject BDoor14;
    55.     public GameObject BDoor15;
    56.     public GameObject BDoor16;
    57.     public GameObject BDoor17;
    58.     public GameObject Question1;
    59.     public GameObject Question2;
    60.     public GameObject Question3;
    61.     public GameObject Question4;
    62.     public GameObject Question5;
    63.     public GameObject Question6;
    64.     public GameObject Question7;
    65.     public GameObject Question8;
    66.     public GameObject Question9;
    67.     public GameObject Question10;
    68.     public GameObject Question11;
    69.     public GameObject Question12;
    70.     public GameObject Question13;
    71.     public GameObject Question14;
    72.     public GameObject Question15;
    73.     public GameObject Question16;
    74.     public GameObject Question17;
    75.  
    76.     public GameObject[]Questions = new GameObject[20];
    77.     public int count;
    78.  
    79.     // Use this for initialization
    80.     void Start () {
    81.         Qtext = Qtext.GetComponent<Text>();
    82.         LDoor1 = GameObject.Find("LCube (1)");
    83.         LDoor2 = GameObject.Find("LCube (2)");
    84.         LDoor3 = GameObject.Find("LCube (3)");
    85.         LDoor4 = GameObject.Find("LCube (4)");
    86.         LDoor5 = GameObject.Find("LCube (5)");
    87.         LDoor6 = GameObject.Find("LCube (6)");
    88.         LDoor7 = GameObject.Find("LCube (7)");
    89.         LDoor8 = GameObject.Find("LCube (8)");
    90.         LDoor9 = GameObject.Find("LCube (9)");
    91.         LDoor10 = GameObject.Find("LCube (10)");
    92.         LDoor11 = GameObject.Find("LCube (11)");
    93.         LDoor12 = GameObject.Find("LCube (12)");
    94.         LDoor13 = GameObject.Find("LCube (13)");
    95.         LDoor14 = GameObject.Find("LCube (14)");
    96.         LDoor15 = GameObject.Find("LCube (15)");
    97.         LDoor16 = GameObject.Find("LCube (16)");
    98.         LDoor17 = GameObject.Find("LCube (17)");
    99.  
    100.         RDoor1 = GameObject.Find("RCube (1)");
    101.         RDoor2 = GameObject.Find("RCube (2)");
    102.         RDoor3 = GameObject.Find("RCube (3)");
    103.         RDoor4 = GameObject.Find("RCube (4)");
    104.         RDoor5 = GameObject.Find("RCube (5)");
    105.         RDoor6 = GameObject.Find("RCube (6)");
    106.         RDoor7 = GameObject.Find("RCube (7)");
    107.         RDoor8 = GameObject.Find("RCube (8)");
    108.         RDoor9 = GameObject.Find("RCube (9)");
    109.         RDoor10 = GameObject.Find("RCube (10)");
    110.         RDoor11 = GameObject.Find("RCube (11)");
    111.         RDoor12 = GameObject.Find("RCube (12)");
    112.         RDoor13 = GameObject.Find("RCube (13)");
    113.         RDoor14 = GameObject.Find("RCube (14)");
    114.         RDoor15 = GameObject.Find("RCube (15)");
    115.         RDoor16 = GameObject.Find("RCube (16)");
    116.         RDoor17 = GameObject.Find("RCube (17)");
    117.  
    118.         BDoor1 = GameObject.Find("BCube (1)");
    119.         BDoor2 = GameObject.Find("BCube (2)");
    120.         BDoor3 = GameObject.Find("BCube (3)");
    121.         BDoor4 = GameObject.Find("BCube (4)");
    122.         BDoor5 = GameObject.Find("BCube (5)");
    123.         BDoor6 = GameObject.Find("BCube (6)");
    124.         BDoor7 = GameObject.Find("BCube (7)");
    125.         BDoor8 = GameObject.Find("BCube (8)");
    126.         BDoor9 = GameObject.Find("BCube (9)");
    127.         BDoor10 = GameObject.Find("BCube (10)");
    128.         BDoor11 = GameObject.Find("BCube (11)");
    129.         BDoor12 = GameObject.Find("BCube (12)");
    130.         BDoor13 = GameObject.Find("BCube (13)");
    131.         BDoor14 = GameObject.Find("BCube (14)");
    132.         BDoor15 = GameObject.Find("BCube (15)");
    133.         BDoor16 = GameObject.Find("BCube (16)");
    134.         BDoor17 = GameObject.Find("BCube (17)");
    135.  
    136.         Question1 = GameObject.Find("CQuestionBar1");
    137.         Question2 = GameObject.Find("CQuestionBar1 (2)");
    138.         Question3 = GameObject.Find("CQuestionBar1 (7)");
    139.         Question4 = GameObject.Find("CQuestionBar1 (9)");
    140.         Question5 = GameObject.Find("CQuestionBar1 (3)");
    141.         Question6 = GameObject.Find("CQuestionBar2 (3)");
    142.         Question7 = GameObject.Find("CQuestionBar1 (4)");
    143.         Question8 = GameObject.Find("WQuestionBar2 (4)");
    144.         Question9 = GameObject.Find("WQuestionBar2 (1)");
    145.         Question10 = GameObject.Find("WQuestionBar2");
    146.         Question11 = GameObject.Find("WQuestionBar2 (6)");
    147.         Question12 = GameObject.Find("WQuestionBar1 (1)");
    148.         Question13 = GameObject.Find("WQuestionBar2 (7)");
    149.         Question14 = GameObject.Find("WQuestionBar2 (8)");
    150.         Question15 = GameObject.Find("WQuestionBar1 (6)");
    151.         Question16 = GameObject.Find("WQuestionBar1 (5)");
    152.         Question17 = GameObject.Find("WQuestionBar2 (5)");
    153.  
    154.         Questions[0] = Question1;
    155.         Questions[1] = Question2;
    156.         Questions[2] = Question3;
    157.         Questions[3] = Question4;
    158.         Questions[4] = Question5;
    159.         Questions[5] = Question6;
    160.         Questions[6] = Question7;
    161.         Questions[7] = Question8;
    162.         Questions[8] = Question9;
    163.         Questions[9] = Question10;
    164.         Questions[10] = Question11;
    165.         Questions[11] = Question12;
    166.         Questions[12] = Question13;
    167.         Questions[13] = Question14;
    168.         Questions[14] = Question15;
    169.         Questions[15] = Question16;
    170.         Questions[16] = Question17;
    171.         count = 17;
    172.  
    173.  
    174.     }
    175.    
    176.     // Update is called once per frame
    177.     void Update () {
    178.    
    179.     }
    180.     public void FalsePressed()
    181.     {
    182.        int number = WhichQuestion();
    183.         if(number == -1)
    184.         {
    185.             Qtext.text = "Something went wrong";
    186.         }
    187.         else {
    188.             if(number == 0)
    189.             {
    190.                 RDoor1.SetActive(true);
    191.                 BDoor1.SetActive(true);
    192.             }else if(number == 1)
    193.             {
    194.                 RDoor10.SetActive(true);
    195.                 BDoor3.SetActive(true);
    196.             }else if(number == 2)
    197.             {
    198.                 RDoor13.SetActive(true);
    199.                 BDoor6.SetActive(true);
    200.             }else if(number == 3)
    201.             {
    202.                 RDoor11.SetActive(true);
    203.                 BDoor8.SetActive(true);
    204.             }else if(number == 4)
    205.             {
    206.                 RDoor14.SetActive(true);
    207.                 BDoor5.SetActive(true);
    208.             }else if(number == 5)
    209.             {
    210.                 RDoor7.SetActive(true);
    211.                 BDoor15.SetActive(true);
    212.             }else if(number == 6)
    213.             {
    214.                 RDoor15.SetActive(true);
    215.                 BDoor13.SetActive(true);
    216.             }else if(number == 7)
    217.             {
    218.                 RDoor6.SetActive(true);
    219.                 BDoor16.SetActive(true);
    220.             }else if(number == 8)
    221.             {
    222.                 RDoor8.SetActive(true);
    223.                 BDoor14.SetActive(true);
    224.             }else if(number == 9)
    225.             {
    226.                 RDoor17.SetActive(true);
    227.                 BDoor9.SetActive(true);
    228.             }else if(number == 10)
    229.             {
    230.                 RDoor2.SetActive(true);
    231.                 BDoor10.SetActive(true);
    232.             }else if(number == 11)
    233.             {
    234.                 RDoor9.SetActive(true);
    235.                 BDoor2.SetActive(true);
    236.             }else if(number == 12)
    237.             {
    238.                 RDoor16.SetActive(true);
    239.                 BDoor2.SetActive(true);
    240.             }else if(number == 13)
    241.             {
    242.                 RDoor3.SetActive(true);
    243.                 BDoor11.SetActive(true);
    244.             }else if(number == 14)
    245.             {
    246.                 RDoor12.SetActive(true);
    247.                 BDoor7.SetActive(true);
    248.             }else if(number == 15)
    249.             {
    250.                 RDoor4.SetActive(true);
    251.                 BDoor4.SetActive(true);
    252.             }else if(number == 16)
    253.             {
    254.                 RDoor5.SetActive(true);
    255.                 BDoor17.SetActive(true);
    256.             }
    257.         Qtext.text = "Then choose the left path!";
    258.         Time.timeScale = 1;
    259.         }
    260.     }
    261.     public void TruePressed()
    262.     {
    263.         Qtext.text = "Then choose the right path!";
    264.        
    265.         Time.timeScale = 1;
    266.     }
    267.     public int WhichQuestion()
    268.     {
    269.         int num = -1;
    270.  
    271.         for(int i = 0; i < count; i++)
    272.         {
    273.             if(Questions[i].gameObject.activeInHierarchy == false)
    274.             {
    275.                 num = i;
    276.                 break;
    277.             }
    278.         }
    279.         return num;
    280.     }
    281. }
    282.  
    would love to get quick responses!!
     
  2. CashCache

    CashCache

    Joined:
    May 16, 2016
    Posts:
    11
    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.
     
  3. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    Why are you using 68 hardcoded fields instead of just 4 lists? Also, why are they public when you initialize them in Start()?
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    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.
     
  5. anasslman

    anasslman

    Joined:
    Sep 5, 2017
    Posts:
    1
    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 ()
    upload_2018-2-7_7-55-40.png upload_2018-2-7_7-56-1.png
    upload_2018-2-7_7-57-5.png
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    When you want to post code on the forums, it's better to post it directly and not a screenshot: https://forum.unity.com/threads/using-code-tags-properly.143875/

    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. :)