A Second Chance: Adding addition lives (Game Dev day 37)

Jack Leavey
3 min readSep 23, 2021

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

Name it whatever you want.

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.

Sorting Layer = Foreground
The extra life collectable.

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.

This is almost identical to how we programmed damage, but we added a life instead of subtracting one.

In the Powerup Script we have to add another case to our array, as seen below.

This list keeps getting longer.

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.

Line 54, increased second integer from 4 to 5, allowing for all 5 possible powerups to be spawned in.

In the below example, the player started with two lives.

Success! We gained a life back.

Now we can see if they dynamically spawn in with the Spawn manager.

First spawn was an extra life!

--

--

Jack Leavey

I am a software engineer with years of experience branching into game development, specifically in Unity. Follow along for guides on creating game mechanics!