A Second Chance: Adding addition lives (Game Dev day 37)
Objective: Add an extra life powerup that will restore one of the player’s missing lives, capped at three.
First, create the new gameobject by dragging the sprite into the Hierarchy
Next, because I used the same sprite as the player’s ship, I shrank it down and added a Halo effect. You also should make sure it appears in the Foreground layer.
Next, we add the Powerup script to this item.
Give it a 2D collider and a RigidBody 2D. Make sure to set “Gravity Scale” to 0.
Make this a prefab before moving on. Any further changes should be done to the prefab.
This powerup has to increase the _lives variable inside the Player script, so add a new function to the script that will increase the _lives variable. Also, make sure to tell the UImanager to update the Lives UI element.
In the Powerup Script we have to add another case to our array, as seen below.
Now we need to assign an ID for the Extra_Life powerup in the spawn manager Gameobject.
Finally, we increase the range on the Spawn Manager Script to allow for this new possibility.
In the below example, the player started with two lives.
Now we can see if they dynamically spawn in with the Spawn manager.