Player Lives & Respawns

Ever asked yourself when playing a game, well if I die here I can just respawn and I’ll be right back to where I just died or wherever I saved last. Well that is not automatically part of a game that has to be programmed into it and I’m here to show how it is done.

First I’m going to add to the UI some text to display our life counter. I’m going to place it right below the CoinCounter that was previously created and anchor it to the top left corner and set the text to display lives. In the UIManager, I will be following the same approach as previously done in the lecture about updating the coins. Creating a private text element and an updateLivesDisplay method.

The player script will also need to be updated as the lives count is kept track of in that script, and the updateLivesDisplay method must be called as well.

Finally link the text to our UIManager.

Now when our player dies there needs to be a respawn point for our player. Create an empty game object and rename it respawn point Zero it out and attach it as a child to our player and then remove it from the player.

I also need to create a dead zone which is the area that when the player falls off the map triggers that the player has died and that it needs to respawn at the respawn point.

Now when the player has used up all 3 lives the level starts over using scene management. Keeping track of those lives is kind of important not all players are cats.

Unity Developer