So I ran into a simple but annoying problem when I was trying to hurry up and finish a game I have been working on for a while so I could move on to start a new idea. I was being stupid an hard coding the last features I wanted to include in the game, and of course came across a problem that I spent 30 minutes on that I could of easily avoided if I was thinking.
Here is how the problem began. I needed a way to keep up with all the Light projectors in my scene and figure out which ones are close to me. This sounded easy enough, but alas I managed to screw it up.
The code I wrote used an array that was set on the awake method using GameObject.FindGameObjectsWithTag("TagName"); In my scene, the player is already in my scene, and the lights are not. When my scene is played, a script called GameInit calls the city generator which in turn calls the multiple factories, one being the factory that generates all the lights in my game. Although it looks like when my scene starts up everything is instantiated at once and all called together, its not. The players awake is called long before the lights are created.
Not only initialization was the problem, but lights could be destroyed throughout game play, which would call for updating the array. In the end of the 30 minutes I went with continually updating the array with the update routine.
This brings up a concept in the book I've been reading that talks on the subject some of Choosing Binding Time Consciously. Whether its updating *shudder* global variables, or any other variables that are being constantly being changed for other scripts to pull from, be careful. Especially in initialization of your code.
Here is how the problem began. I needed a way to keep up with all the Light projectors in my scene and figure out which ones are close to me. This sounded easy enough, but alas I managed to screw it up.
The code I wrote used an array that was set on the awake method using GameObject.FindGameObjectsWithTag("TagName"); In my scene, the player is already in my scene, and the lights are not. When my scene is played, a script called GameInit calls the city generator which in turn calls the multiple factories, one being the factory that generates all the lights in my game. Although it looks like when my scene starts up everything is instantiated at once and all called together, its not. The players awake is called long before the lights are created.
Not only initialization was the problem, but lights could be destroyed throughout game play, which would call for updating the array. In the end of the 30 minutes I went with continually updating the array with the update routine.
This brings up a concept in the book I've been reading that talks on the subject some of Choosing Binding Time Consciously. Whether its updating *shudder* global variables, or any other variables that are being constantly being changed for other scripts to pull from, be careful. Especially in initialization of your code.
Comments
Post a Comment