Search Unity

NullReferenceException on OnEnable function

Discussion in 'Scripting' started by Kalita2127, Jun 18, 2019.

  1. Kalita2127

    Kalita2127

    Joined:
    Dec 6, 2014
    Posts:
    279
    Hi this maybe a silly question but this is the first time I met this error when using OnEnable function. So the script is like this:
    Quiz.cs
    Code (CSharp):
    1. private void OnEnable()
    2.     {
    3.         QuizManager.I.Reposition(rt);
    4.         QuizManager.I.MakeQuiz(this);
    5.     }
    QuizManager.cs
    Code (CSharp):
    1. public void MakeQuiz(Quiz quiz)
    2.     {
    3.         TileType[] ttArr;
    4.         ShuffleQuiz(out ttArr);
    5.  
    6.         quiz.displayID = (int)ttArr[0];
    7.         quiz.displayImg.sprite = collection.GetSpriteFromTileID(ttArr[0]);
    8.  
    9.         quiz.qP[0].choiceID = (int)ttArr[1];
    10.         quiz.qP[1].choiceImg.sprite = collection.GetSpriteFromTileID(ttArr[1]);
    11.  
    12.         quiz.qP[1].choiceID = (int)ttArr[2];
    13.         quiz.qP[2].choiceImg.sprite = collection.GetSpriteFromTileID(ttArr[2]);
    14.     }
    it said I have null reference on
    QuizManager.I.MakeQuiz(this);
    so I wrote a line of code on Awake() function
    q = GetComponent<Quiz>();
    and passed it to MakeQuiz function but no luck.
    Any help will be appreciated. Thanks
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Use Log.Debug to make sure all the separate parts you are accessing are really present:

    Code (CSharp):
    1.  
    2. private void OnEnable()
    3. {
    4.     Debug.Log("QM is " + QuizManager.name);
    5.     Debug.Log("I is " + QuizManager.I.name);
    6.     Debug.Log("RT is " + rt.name);
    7.     QuizManager.I.Reposition(rt);
    8.     Debug.Log("First Access Done");
    9.     QuizManager.I.MakeQuiz(this);
    10. }
    11.  
    I'm betting it's not 'this' that causes the problem, but "I" or "QuizManager"
     
  3. Kalita2127

    Kalita2127

    Joined:
    Dec 6, 2014
    Posts:
    279
    My bad I made a simple mistake. The problem was in the variable
    displayImg
    I put [HideInInspector] that make the reference null. I think I need some coffee
     
  4. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Glad to hear that it is working now.

    A small aside that you probably know already:

    HideInInspector will not make the contents of a variable null. It merely makes it inaccessible via the Inspector. But since displayImg is a class variable, it is initialized upon class instancing. If you did not initialize that variable to a default value during declaration, it will be initialized to null.

    You can verify this by trying the following:

    Code (CSharp):
    1. [HideInInspector]
    2. List<string> L = new List<string>();// initialize this variable to a valid List
    and then a access L - you'll find it will NOT be null.
     
    Kalita2127 likes this.