Adding A Start Event To My Game


I start by creating the asteroid game object itself. I drag my sprite into the scene view and populate it with components so that till be able to detect collisions.


We create a new script called Asteroid. This script will handle the asteroid’s rotation, collision, communication to the spawn manager script, instantiating the explosion effect, and finally destroying itself.
It saved time to just instantiate the effect rather than creating a animation state for it. In the future, ill most likely create a animation for it rather than coding it in.
I disable the collider in case the lasers make multiple contact with the asteroid. If that happens, our co routines may trigger multiple times. This is bad, considering that our co routines are running while loops. Ive had enough nightmares in C++ to know what 500+ while loops can do to my PC. I didn't actually test if it did, but i didn't want to take my chances.


We then create the explosion game object, give it a looping animation, and its own script that will call the destroy function on start. Lastly, prefabbing it so our asteroid script may instantiate it.

The destroy script that may be universally used on any game object.

Lastly, we head back into our SpawnManager script and remove the StartCoroutine() functions out of start. We create a public function called StartSpawning() which is called when the asteroid has detected another collider with a tag of ,’Laser’.
Both coroutines are given an additional yield command to wait for 1 second. This will allow us to give some time for the asteroid to finish exploding and wont give our player a heart attack when the enemies start instantaneously pouring in.

I can wade around and get used to the controls before being thrown into the fray. This will serve as a stepping stone for something more creative in the future.