Game A Week 5: SpaceShips Go Pew Pew (Part 2)

In part 1 I presented the game and analyzed its development up until the last 6 hours when I hit a wall. In this part I describe the problems I had during these final hours of development and what future plans I have for the game.

Development < T – 6 hours

So after having everything go pretty much smoothly in the project, I encountered a problem. All games have a GameManager to handle things like scoring, changing levels and in-game events. In the case of this project, to have the GameManager know when and how the level should end, it would need a boolean value from both the Player and EnemyGrid scripts to tell it whether the player or enemies are dead. To do this it would have to have a DontDestroyOnLoad() function call in it’s initialization so it would persist in the background when a new scene (think level) was loaded. Normally the GameObjects in each scene are completely separate from each other, and when a new scene is loaded the old ones are destroyed and new ones are loaded, but the DontDestroyOnLoad() function makes the GameObject the script is assigned to persist through all scenes. While this sounds very useful it caused some problems because even though the GameManager persisted, the object references to the Player and EnemyGrid scripts it had were destroyed when a new level was loaded. I.e. the object references were GameObject-dependent. This meant basically that after the first level none of the following levels worked.

I was determined to do the project “my way” and I decided that I had to get the object references dynamically. This meant that the GameManager script would have code that would basically find any objects tagged “Player” and “Enemy” and get access to their scripts (and therefore variables) through that. So that way every time a new level was loaded it would basically find the new object references by itself. But I didn’t get them to work! For hours I tried many combinations of system functions to find them… FindObjectsWithTag(), FindGameObjectsWithTag(), FindObjectsOfType() followed by GetComponent<TypeName>(). Nothing worked! It seemed to find the objects themselves but it just couldn’t access the scripts. It’s still beyond me. The code compiled nicely every time and everything seemed fine until the GameManager tried to access something from the new GameObjects. “Object reference not set to an instance of an object”. Repeatedly.

I even asked about it in Stack Exchange, but the guy who was helping me couldn’t determine the problem. And neither did I. But he did give good advice that was already too late by then. It would have been better to have the GameManager create the Player and EnemyGrid (with instantiated enemies) every time a new scene was loaded that wasn’t the main menu. I also read somewhere that a good idea is to never have the GameManager in the background at all. The script itself should only be accessed and used in a separate loading screen. This loading screen should be a separate scene that was only always loaded after a level ends and another one begins. So the way I approached the problem was pretty much flawed by design.

In the final moments I scrambled to have at least something semi-working to release. A few bugs were left in, though, as a legacy from the GameManager-object-reference-fiasco.

The Future

In its current form, the game only has the one level and I added some GUI to tell the score after the game ends. For the future I’m thinking of fixing the currently present bugs in the ways I described above and then adding some new features, enemy types and levels. I also really want to make some animations for the player and other enemies! And maybe some powerups!

I like that now that it’s no longer a Game A Week submission I don’t have a time constraint to push to. However that will probably lower the pace of development quite a bit. Hopefully I will find some second wind once I fix all the bugs and things start rolling again with new features.

Welp, now I just have to come up with something for next Sunday. See ya, then!

Leave a comment