Search Unity

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

Discussion in 'Scripting' started by mebaucco, Jul 11, 2018.

  1. mebaucco

    mebaucco

    Joined:
    Mar 6, 2018
    Posts:
    5
    Hello,

    I am trying to work through the video on saving and loading at this link but I am running into a problem. In the video they could access variables from the GameControl object by using a public static reference called control which allowed them an easy way to get to those variables from other classes(?). To test this, I made two scripts:

    Code (CSharp):
    1. using System;
    2. using System.IO;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using System.Runtime.Serialization.Formatters.Binary;
    6. using UnityEngine;
    7.  
    8. public class GameControl : MonoBehaviour {
    9.  
    10. //this will be in the only GameController for the whole game. Add the following for easy access from anywhere:
    11.     public static GameControl control;
    12.  
    13.     public int foo = 42;
    14.  
    15. }
    And then I made another script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Star : MonoBehaviour {
    6.     //stars need a name, a color, and a size in addition to their transform
    7.     public string sName {get; protected set;}
    8.     //colors can't be serialized by the C# save format, have to break it into rgba values
    9.     public Color cColor { get; protected set; }
    10.     public float fSize { get; protected set; }
    11.     public int nTest;
    12.     public void SetTest()
    13.     {
    14.         nTest = GameControl.control.foo;
    15.         Debug.Log ("nTest is " + nTest);
    16.     }
    17.  
    18.     void Start()
    19.     {
    20.         SetTest ();
    21.     }
    22. }
    23.  
    When I run it I get the error mentioned in the title. As far as I can tell I'm doing everything the video did, can anyone show me where I am going wrong?

    Thanks in advance,
    Matt
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You should state what script and line number the error happens at. But all null reference errors are resolved in the same manner.

    1) Go to the line number of the first null reference error and determine what variables used on that line are reference types - if there is only 1 reference type variable, skip to step 3
    2) Determine which variable from that line is null by adding debugging before the error line (Debug.Log, etc), or split up that line of code so you are only using a single reference type variable per line
    3) Step backward through your code and figure out why the variable is null when you obviously expected it to not be null when you tried using it - this often involves adding additional debugging at various points
    4) Make changes so as the variable is no longer null by the time you use it - Done!
     
  3. mebaucco

    mebaucco

    Joined:
    Mar 6, 2018
    Posts:
    5
    I managed to find a video that showed a working version of what I was trying to do. Just in case anyone needs it, here is the new code:

    Code (CSharp):
    1. using System;
    2. using System.IO;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using System.Runtime.Serialization.Formatters.Binary;
    6. using UnityEngine;
    7.  
    8. public class GameControl : MonoBehaviour {
    9.  
    10. //this will be in the only GameController for the whole game. Add the following for easy access from anywhere:
    11.     public static GameControl Control{get; private set;}
    12.  
    13.     public int foo = 42;
    14.  
    15.     void Awake()
    16.     {
    17.         if (Control == null)
    18.         {
    19.             Control = this;
    20.             DontDestroyOnLoad (gameObject);
    21.         }
    22.         else
    23.         {
    24.             Destroy (gameObject);
    25.         }
    26.  
    27.     }
    28. }
    29.  
    and then the other:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Star : MonoBehaviour {
    6.  
    7.     public int nTest;
    8.  
    9.     void Start()
    10.     {
    11.         nTest = GameControl.Control.foo;
    12.     }
    13.  
    14.     void Update()
    15.     {
    16.         Debug.Log (nTest);
    17.     }
    18. }
    19.  
    Now I can access GameControl.Control.foo; from any other script. I don't know whether things have changed since the original video was made, but this worked for me.

    Thanks,
    Matt