Quote of the Day: “Turns out it was simpler than I thought” – Pretty much every programmer at some point.
Music of the Day: Mostly The Who with little sprinkles of Crüe tossed in.
Today was a roller coaster. I came in with the mental state of “OK, I actually need to do stuff this week,” and pretty immediately ran into problems. I was again focusing on the teleporter, and trying to fix the issue from last week where those two specific bots couldn’t attack after going through. I tried a few things and couldn’t really get anything to work, so I decided to take a closer look at the error and go through step by step.
If you’ll remember, the error that Unity was throwing was a NullReference, which means it was looking for something that wasn’t there. I knew what line it was throwing the error at (You can double click on the error and it will show you in the code), but I could not for the life of me figure out why. The entire robot object, attack script and all, was in the DontDestroyOnLoad category, which means it persists between levels unless you tell it otherwise. SO all of the data that it intakes when it’s first initialized should remain there in every single level. So, taking a page out of my theater tech book, I worked the problem in reverse.
In theater tech, especially with lighting and sound, if something isn’t working you go through step by step and check each individual component, starting with the simplest and moving to the most complex. So with lighting for example, if a light isn’t turning on, you first check if it has power, if the power itself is on, if the cable is faulty, if the lamp has burned out in the fixture, and then move on to more complicated and harder to fix stuff, like are the power connections sound or is the lens burnt (You also check the shutters. I once had the embarrassment of going through all of that only to realize I still had the shutters in. never made that mistake again).
So in my code, I added Debug.Log statements to strategic parts. Debug.Log statements print out a message or value to the console so that you can easily read it, and are super useful for making sure that everything is called in the right order. So I worked through linearly, making sure that each individual part was being called and executed when they needed to be. And they were. So once again, I was at a loss. The only other thing I could think to do was check the individual variables, and lo and behold, I found the problem.
The problem: the Start() method is only called one time, and not when a new level is loaded. The non-jargon version of the problem: The attack is only set up the first time the bot is put into a level, not the next time. So in the first level, everything is peachy and works as it should. But after teleporting, the script neglects to find all of the necessary components again, so the game gets confused when there’s nothing for it to retrieve. The solution: use OnLevelWasLoaded, which, true to its name, calls things when a level is loaded. This still presented problems, because now all of that data was initialized in later levels, but not the first. So the final solution was to use both Start and OnLevelWasLoaded to make sure everything is set up and ready to go. With that done I finally had a fully functional teleporter for single player survival, and was ready to move on.
I only had about 45 minutes before I needed to leave, so Jeremy asked me to fix a small menu issue with highlighting choices. If you’ve ever played a game (or for that matter used any program with a User INterface), you’ve probably seen how buttons are highlighted when you hover over them. In our case, this worked fine for a mouse but was having problems when you tried to select things by controller. This was a pretty boring fix, I just had to make a call that happened before anything else to highlight the currently selected menu option, and then copy and paste it about 50 times for every single menu button across the game. And with that done, i was out for the day.
If you’ve made it this far, congratulations! If I make a post tomorrow it will probably be pretty short, because it’s my birthday and I doubt I will want to go to work for a long time. And I will be gone on Wednesday, a Green Day Concert awaits me. But I will be back on Thursday! Thanks for reading.