Code (CSharp): _player = GameObject.FindWithTag("Player").GetComponent<Player>(); if (_player == null) Debug.LogWarning("Enemy Player is null."); _playerOne = GameObject.FindWithTag("Player One").GetComponent<Player>(); if (_playerOne == null) Debug.LogWarning("Enemy Player One is null."); _playerTwo = GameObject.FindWithTag("Player Two").GetComponent<Player>(); if (_playerTwo == null) Debug.LogWarning("Enemy Player Two is null."); This is my code. I have two scenes in my game, one where there is a single player with the tag player, and the other where I have two players with the respective other tags. thing is, I've discovered after a lo of debugging that the code runs till these lines, then a NullExceptionError occurs and the code begins from the, well beginning. How can I make them iterate over, for I was under the assumption that even if a null value came, the value will just be set as null and the game will continue to the next line. What exactly is the problem here?
Code (CSharp): _player = GameObject.FindWithTag("Player").GetComponent<Player>(); That line right there. If GameObject.FindWithTag() doesn't find anything it will be null. Trying to call GetComponent() on a null object will give you a null reference exception. Code (CSharp): _player = GameObject.FindWithTag("Player"); if (!_player)//Does the same as _player == null Debug.LogWarning("Enemy Player is null.");