S
S
Sergey Bezrukov2019-07-20 20:52:49
C++ / C#
Sergey Bezrukov, 2019-07-20 20:52:49

Why does the console throw a NullReferenceException in this case?

The console throws an error when the game ends. "Object instance reference not set." It swears at this piece of code:

public void OnDestroy()
{
  if (Application.isPlaying)
  {
    InputManager.instance.RemoveWeapons(this);
  }
}

The bottom line is that the object on which the script hangs can be deleted during the game. But also the object is removed when the scene is deactivated. When the scene is deactivated, no actions need to be performed, so there is a condition that should protect against this:
if (Application.isPlaying) { }
Added the following line to the condition: When the game ends, true
Debug.Log(Application.isPlaying);
is constantly displayed in the console , although, as I understand it, this property should return false at this moment . Also, one of several times, other things being equal, no error message is displayed, but it is still true . What could be the issue here? I hope I described it in a readable way. Thank you in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GavriKos, 2019-07-20
@Hrederik

The point is exactly that you have something equal to null.
What exactly - the debugger will show, but 99% that this is an instance of the InputManager.
scene deactivation has nothing to do with this property. Read the manual.
To solve the problem, there are two options:
- check instance for null
- reconsider the architecture so that there are no such calls.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question