Have your character cycle through lives

Objective: When your player dies, respawn without restarting the level.

The trick to this is you don’t destroy your player. Too many things break when that happens. What we want to do is disable the player and then re-enable it. First, we remove the destroy option. We then want to try to turn everything off. We added a _youDied bool that is applied to our update method so you can’t shoot or move when dead.

Then we added a function that goes to a game manager. This new object will control our game’s function.

Next, we want the UI manager to spawn the player. We want it to update the lives counter — 1 and also run a restart player script on our player.

The restart player script will invert the destroy features such as turning back on the colliders, the renderers, etc.

Once we get the actual death working, we ant to make sure we don’t spawn the player immediately but rather let the player have a couple seconds to breathe. Then come back with invincibility turned on. This comes in the form of a coroutine. A little messy jumping from one component to another, but it works.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Al Heck

Unity Developer and Software Engineer who loves making games with GameDevHQ