-4

I am trying to troubleshoot the following piece of code which causes a NullReferenceException. Essentially when an object is created, I'm trying to have it register with my game manager class. This is the component for my object:

void Start()
{
    Debug.Log("Registering");
    if (gameObject != null)
    {
        GameMngr.Instance.RegisterAttraction(gameObject);
    }
    else
    {
        Debug.Log("Gameobject null");
    }
}

In my game manager I have the following:

public void RegisterAttraction(GameObject newAttraction)
{
    if (newAttraction != null)
    {
        Debug.Log("Attempting to register gameObject");
        attractionLastID++;
        sceneAttractions.Add(attractionLastID, newAttraction);
        Debug.Log("Registered");
    }
    else
    {
        Debug.Log("unable to register: null provided");
    }
}

My console output is as following:

  • Registering
  • Attempting to register gameObject
  • NullRefereceException

The fact that my code displays the attempting to register gameObject lines leads me to believe that my newAttraction variable is not null. Why do I get the error ?

Thank for the help

derHugo
  • 83,094
  • 9
  • 75
  • 115
user387302
  • 395
  • 1
  • 4
  • 13
  • print the gameobject instanceID , maybe it is not a same gameobject ?? – TimChang Sep 27 '19 at 02:55
  • _"NullReferenceException problem in Unity when object is not null"_ -- the idea that you might get `NullReferenceException` when the reference is not in fact null is just nonsensical. The null value may be something other than the one you think it should be. But there is *definitely* a null value that you're trying to dereference. See proposed duplicate for complete details on how to debug the problem. – Peter Duniho Sep 27 '19 at 02:55
  • `GameMngr.Instance.RegisterAttraction(gameObject);` How to sure that it is set from your script ? , mabye it is from other script and you think it is from your script. – TimChang Sep 27 '19 at 03:00
  • also make sure you [**never** use `== null`/`!= null` for references of type `Object`](https://stackoverflow.com/a/58069056/7111561) in Unity! – derHugo Sep 27 '19 at 07:06

1 Answers1

2

Where's attractionLastId defined by chance? Is it being initialized? Since you're doing a null check inside of your Start function already, try refactoring your RegisterAttraction function to look something like:

public void RegisterAttraction(GameObject newAttraction)
    {

            Debug.Log("Attempting to register gameObject");
            attractionLastID++;
            sceneAttractions.Add(attractionLastID, newAttraction);
            Debug.Log("Registered");
    }

I highly recommend setting a breakpoint on the beginning curly of RegisterAttraction. Take it step by step and hover over each variable to see which one is null.

Jee Mok
  • 6,157
  • 8
  • 47
  • 80
dnunez32
  • 224
  • 1
  • 9
  • Turns out that the issue was with sceneAttractions - it wasn't initialized early enough. Thanks for suggesting the breakpoint! – user387302 Sep 27 '19 at 03:15