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