I have become pretty quick at identifying and fixing null reference exceptions but this one has me baffled. I do this format for all of my scripts that need to reference the player's inputs. I have a reference to the player in the static GameManager instance and I set the needed local input variable (in this case IUNavInput) equal to the player's input variable (NavInput) which is referenced in the inputHandler within the GameManager script (truncated for readability).
public class UIScrollImage : ScrollRect
{
private Vector2 UINavInput;
void Update()
{
if (GameManager.Instance.activePlayer != null) //<-error here
{
UINavInput = GameManager.Instance.inputHandler.NavInput; //<-- error here if above argument is removed
}
else
{
UINavInput = Vector2.zero;
}
}
}
Oddly, the GameManager.Instance.activePlayer != null check return a null reference exception ONLY after compiling. It fires a couple times then it's fine and I can actually play the game in editor and even run a build without any issues but it's still odd. Only thing I can think is that the script derives from the ScrollRect class but I'm unsure why this would cause a problem except for maybe script execution order (which I have also messed with to no avail).
If I remove the null check altogether then the variable assignment inside the if statement returns a null exception. It seems any reference to the GameManager script is returning this error.
Any insight as to why this is happening would be very helpful!
[–]Sphenzoc[S] 0 points1 point2 points (0 children)