Hey and welcome!
Ok I enjoyed implementing this one so I’ve got a fun one for you here where we’re going to set it so that our shield can take 3 extra hits now and the color changes per hit to show the sustained damage.
private int _shieldHealth;
private SpriteRenderer _shieldVisualHealth;public void Damage()
if (collectedShield == true)
_shieldHealth -= 1; switch(_shieldHealth)
collectedShield = false;
_shieldVisualHealth.color = Color.red;
_shieldVisualHealth.color = Color.yellow;
_shieldVisualHealth.color = Color.magenta;
Debug.Log("Something has gone wrong with the Shield Health");
}public void EngageShieldPowerup()
_shield = Instantiate(_shieldPrefab, transform.position, Quaternion.identity);
collectedShield = true;
_shieldVisualHealth = _shield.GetComponent<SpriteRenderer>();
_shieldHealth = 4;
Let’s look at the EngageShieldPowerup() to start with, in here you’ll see that we’re setting our new int variable _shieldHealth to 4 so that we can make our shield take extra hits as we reduce the number by 1 per hit in the Damage() method. Next we’re adding the sprite renderer component of the shield that gets instantiated to our _shieldVisualHealth variable. The sprite renderer component looks like this in the inspector:
With that done we can look at our new switch statement in Damage(), specifically the _shieldVisualHealth.color = Color.magenta; line that gets called when the shield health drops to 3 after taking a hit.
As you can see in the image above it has a Color value which we can access using _shieldVisualHealth.color, with that done we can use a built operation called Color.magenta to set the colour to magenta. Then we proceed to turn the color to yellow and then red before destroying the shield altogether.
Here’s an image of the full list of colour properties available:
Sadly no hot pink or regular pink this time, however if you know the rgba value you can do things this way:
Then instead of assigning the _shieldVisualHealth.color to Color.red you would assign it to newColor instead.
There’s a little sneak peek of what’s to come for the next article in the bottom left there.