Lives and Respawn System in Unity
Objective: create a live system and make the player respawn in a specific position after death.
In the scene I duplicated the coins display and changed the text to use it as a live display.
In the UIManager script I created a variable to store the reference of the live display text and a function to update the value of the display.
Then in the player script I declared a variable for the lives and initialized the text with the UIManager function passing as a parameter the lives of the player.
Then in the player script other variables, a deathZone is the value in the Y axis where the player dies. The respawn position is the position of respawning when the player dies.
In the start method I initialize the respawn position as the initial position of the player in the game. In the update method I check if the player position in the Y axis is lower than _deathZone and if it is true I decrease the number of lives, update the lives display with the function in the UImanager, change the position of the player to the respawn position and finally check if the number of lives is less than 0 to reload the level using the function LoadScene (Make sure you are using the UnityEngine.SceneManagement namespace).
And that’s it!!! We have a lives and respawn system for our game.