Today I made enemies turn red when shot and turn normal when moving again.
This is what I do today:
I create a sprite variable that will contain enemy sprite, I use that to can change enemy’s color.

Then create a function named FlashRed(), this function will change the color of the object to red and after 0.5 seconds will change to white, that is, return to the original color of the object.

And call this function when the bullet is collided with the enemy.

But as you can see above, my yellow spider will turn the same color as my gray spider. Since I have them set to be the same spider, just the size and color change. So when it turns red, it turns back to its original color, which is gray.
To fix this I created a new variable called enemyID. I can provide the enemy ID in unity so I can use it to change the color depending on the enemy ID.

And change the FlashRed() to ChangeColor() with an argument is enemyColor.

When a bullet hits an enemy, check the enemy’s ID. If it’s 1, call ChangeColor() with “white”; if it’s 2, call it with “yellow.”

No Responses