Learning from Bugs

JEFF SHAW
2 min readNov 22, 2020

--

I solved a nice bug today in my Homing Missiles, I say “nice” because like many bugs that are not just a typo, I learned something good. I was perplexed when I kept getting a “NullReferenceException: Object reference not set to an instance of an object” which indicates something is being referenced that was not yet assigned. This was troubling for me because it was occurring in the line of code where I was assigning it highlighted below:

Find with GetComponent fixed by breaking into two parts

Sometimes this would work fine with enemies dropped into the scene to test. I had been doing a very similar find for a component on the Player object with no problem.

As it turned out I realized I needed to check if the Find was working to find the object, before I tried to GetComponent since it didn’t find any object to get the component from. In the case of the Player, the player object was always in the scene so it never failed.

My debugging was further complicated by the fact I was putting some test enemies into the scene at the top level of the hierarchy, and the spawned enemies were were nested in a couple of folders, so the Find was working on the test enemies and not the spawned ones.

So also a lesson on how a problem can be caused by two different issues that were introduced at once. The key to solving this kind of thing seems to be thinking how to break the problem into a simpler form, and keep breaking it down until you can see what is going on.

Another frustrating one, I had missed that IEnumerator had autocompleted as IEnumerable and creating a mystifying error. Since it was an actual MonoDevelop method, it was not immediately identifiable as a typo.

--

--

JEFF SHAW
JEFF SHAW

Written by JEFF SHAW

Unity VR Developer (VRX Virtual Reality Experiences VRX)

No responses yet