Search Unity

Object reference not set to an instance of an object

Discussion in 'Scripting' started by ilikecookies2015, Dec 21, 2018.

  1. ilikecookies2015

    ilikecookies2015

    Joined:
    Oct 27, 2017
    Posts:
    5
    I have this game where my character collides with a door, a text shows up. In the Unity Editor, my game works fine, without any errors. However, once the game has been built as a 'Development Build', the game sends an error as soon as the game starts.

    Here is my Door.cs script, attached to my door collider:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class Door : MonoBehaviour
    8. {
    9.    
    10.     public int LevelToLoad;
    11.  
    12.     private GameMaster gm;
    13.  
    14.     void Start() {
    15.  
    16.         gm = GameObject.FindGameObjectWithTag ("GameMaster").GetComponent<GameMaster> ();
    17.         gm.inputGrp.SetActive (false);
    18.  
    19.     }
    20.  
    21.     void OnTriggerEnter2D(Collider2D col) {
    22.  
    23.         if (col.CompareTag ("Player")) {
    24.  
    25.             gm.inputGrp.SetActive (true);
    26.             gm.inputText.text = ("[E] to enter");
    27.  
    28.             if (Input.GetKeyDown ("e")) {
    29.  
    30.                 SceneManager.LoadScene (LevelToLoad);
    31.  
    32.             }
    33.  
    34.         }
    35.  
    36.     }
    37.  
    38.     void OnTriggerStay2D(Collider2D col) {
    39.  
    40.         if (col.CompareTag ("Player")) {
    41.  
    42.             if (Input.GetKeyDown ("e")) {
    43.  
    44.                 SceneManager.LoadScene (LevelToLoad);
    45.  
    46.             }
    47.  
    48.         }
    49.  
    50.     }
    51.  
    52.     void OnTriggerExit2D(Collider2D col) {
    53.  
    54.         if (col.CompareTag ("Player")) {
    55.  
    56.             gm.inputGrp.SetActive (false);
    57.             gm.inputText.text = (" ");
    58.  
    59.         }
    60.  
    61.  
    62.     }
    63.  
    64. }
    65.  
    the variable inputGrp inside my GameMaster isn't used, it is just referenced through the unity editor.

    Is it just a bug, or should I find a different way to reference my text inputGrp? Thanks you in advance!

    Cheers
     

    Attached Files:

  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    See if you can get more information about the error. It says open.
     
  3. ilikecookies2015

    ilikecookies2015

    Joined:
    Oct 27, 2017
    Posts:
    5
    Here is the file that gets opened when I pres Open Log. It doesn't give any more info the the error. I checked that I didn't forget a reference or anything, and since it works fine in the Unity Editor, I can't figure out what the problem is.
     

    Attached Files:

  4. ilikecookies2015

    ilikecookies2015

    Joined:
    Oct 27, 2017
    Posts:
    5
    Also, the door is meant to load a different scene when I press E, which works fine, the problem seems to be only with the GameObject inputGrp
     
  5. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Your not finding the GameMaster object, this is probably an initialization order error.
     
  6. ilikecookies2015

    ilikecookies2015

    Joined:
    Oct 27, 2017
    Posts:
    5
    so how should i reference the GameMaster to fix this error?
     
  7. ilikecookies2015

    ilikecookies2015

    Joined:
    Oct 27, 2017
    Posts:
    5
    I seem to have found the source of the problem, on line 17, "gm.inputGrp.SetActive (false);", probably because it is in the Start(), with the line that references the GameMaster. Removing that line fixes the errors, but I haven't found an alternate method to do "gm.inputGrp.SetActive (false);"
     
  8. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    You can reorganize the script executing order for one thing, but what i tend to do is use a singleton pattern(not the best thing ever, but it's fine for small projects and solo development), set up the references on Awake and use only them on Start.