Daily progress: Day 2, Extra Life.

Adding in an Extra Life/Damage Repair Pickup.

Rose Owen
4 min readJun 14, 2021

With GameDevHQ, we have been set a task to make this 2D space shooter. At this point in the journey, we have to research how to do a list of tasks to add to this game.

Extra Life Pickup:

This one I think I made things a bit harder on myself, not on purpose though. Since I already had a Damage method for the character using a switch statement, I was thinking I could just use that again. In the end, it works, and there haven’t been any problems so far.

Way back when I added in the visual damage to the player, I had changed how lives were done from if and else if statements to a switch. This ended up making things a bit easier, that part of the damage method didn’t need changing anymore.

Starting with a Public method that would be called for the player when we get the pick up was the starting point.

Why it has the bool there will make sense soon.

By limiting it to when lives are under 3, makes it so it doesn’t get a null reference when lives are 4 and it’s trying to look for a 4th element in the sprites for lives.

Now the fun part…. Adding to the Damage method to be able to use it for both damage and health. By putting a bool Damage in the brackets for the method, this will give the option to limit all of the damaging parts within an if statement and keeping the rest separate. Since we still want to update the lives and the sprites we won’t want to use an else though.

Since the If statement finishes just after the “_lives — ;” we can now use this damage method as a healing method too. First, we will have to fix the methods that just got broken though.

You can click on them to bring you to the line that broke.

After adding True to the damaging methods and False to the Healing methods, remembering to jump over to the enemy’s script to fix that one too. Lastly, jump over to the Powerup Script and add in the new case for the extra life and link it to the extra life method we made. With that, the scripting side of things is done.

Now to set up the Visual side of things. Create an empty and add on the usual, 2D Rigidbody (Set Gravity Scale to 0), 2D Box Collider (Set is Trigger), Powerup script (Set ID and put in the Sound clip), and Sprite Renderer. With the Sprite Renderer, we can use the sprite for the UI’s life. Cutting down to 1 from any of the sprites (apart from the 0 lives one) will give you a useable Sprite for the Renderer.

Lastly, turn it into a prefab and put it into the Spawn Manager. Which you might have already seen in the last article.

Here it is again for those that missed it!
Looking good and working as intended!

And with that, another feature is added. Next time I will be adding a Screen Shake Effect when the player takes damage and since there are so many people that don’t like screen shake I will also add the option to turn it off. See you there!

--

--