Get a Life

Cliff Bailey
2 min readAug 21, 2022

--

Heyo, I’m back!

Let’s do a life system thing.

So, for literally anything to exist in your game — anything at all — you need to create it.

Unless you’re using a template with premade assets and such, that means among other things, you need to create the concept of life and how many lives your player gets before game over.

In the player script, let’s go ahead and declare a variable for the player’s lives:

Then we create a method called Damage under fireLaser method:

Then, we need to call Damage from the Enemy script via script communication:

In the enemy script, OnTriggerEnter gets info of other via interaction with a collider.

other is the variable storing that info.

So when the collider has the Player tag, it stores the game object name Player in the variable other.

To access it, then, we reference the variable, other, via the only component we have direct access to, transform, and then use GetComponent to access more, in this case the script which is called player, and then access the Damage function therein.

Now need to nullcheck — create reference variable:

Were we to use the tag check could potentially lead to errors — this way is cleaner and easier.

Note that the number of lives goes down in the inspector, the enemy is destroyed, and the player persists so long as there are remaining lives.

--

--