My 90 day journey becoming a Unity game developer: Day-39
Objective: Fixing a bug pertaining to destroying our enemies.
When playing the game, it was discovered that we can hit the enemy twice with our lasers and score on each hit before the enemy is destroyed.
What we need to do is turn the colliders on each enemy off. We will use this simple way to fix the problem by going to our Enemy script. Inside the OnTriggerEnter2D() method and in the Laser’s IF condition, we want to destroy the collider itself on each enemy before the enemy is completely destroyed. The enemy is destroyed in approximately 2.4 seconds which means the collider on the enemy is still active until that time runs out. Right before we destroy the Enemy, insert another Destroy() method, but this time we want to get the component Collider and destroy it right away.
Now when we play the game, notice the lasers go through the enemy’s explosion on the second shot. Plus, look at the score and notice only 10 points given for each enemy destroyed.
Hopefully next time I will be able to set up WebGL, because I ran into some serious errors that prevented me from building the game. If not, we will work on randomizing the enemy’s fire rate at the player.