Ok, so this has been an ongoing problem I keep getting and I’d really like to get to the bottom of why it’s happening.
In my platformer, enemies only have one player object to reference in their AI, so I have them run some code in Start() that looks something like this:
pc = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerController>();
This works just fine when I create it and doesn’t cause any problems for months. Then suddenly, after changing some unrelated thing, it stops working. In this instance, all I did was create a new animation for the player character, a new variable for their animator and I hit play to test how the current animations were transitioning so I could make sure I had it working right. Suddenly I’m getting a NullReferenceException because that line of code apparently didn’t find the player object. Except, of course, I haven’t changed anything else. There’s still only one object in the scene with the Player tag, it’s still there and it still has the script attached.
This is really just an annoyance rather than a face-meltingly awful bug, because a quick restart of the Unity editor usually fixes it right up, as it did this time as well. I’m just wondering what could be causing this, and if there’s a better way I should be doing things?
(Strangely, after stopping to edit the transitions in the player animator again, the next time I hit play, the enemy started throwing NullReferenceExceptions again. If this keeps up, this issue is escalating rather quickly…)
Edit: Same happened a third time, and on trying to exit Unity crashed hard and threw a “GetThreadContext failed” error. Maybe my installation is broken?