Introducing Health
Tutorial
·
Beginner
·
+10XP
·
30 mins
·
(225)
Unity Technologies

You will add health and define the winning conditions for the game
Tutorial Outcome
By the end of this tutorial, you will be able to
- Add script to keep track of the health of a player
- Make obstacles subtract a player’s health
- Display the health of a player
- Add User Interface design to the game
- Tag GameObjects
- Define the winning conditions
Key concepts:
- Health System Attribute script
- Modify Health Attribute script
Challenge Ideas:
- Try introducing a GameObject that allows the player to recover points. Hint: it’s really important to use a positive value for the Modify Health Attribute script’s Health Change section
Languages available:
1. Introduce Health

Modifying the Health Change value will change the numerical value by which the player is affected when it collides with an obstacle
You’ll introduce health so that objects can damage each other on collision
- While clicking on the SpaceshipRed in the Hierarchy, go to the Inspector window and add the Health System Attribute script to keep track of the health of the player.
- Go to Hierarchy and click on the asteroid. Now, in this Inspector window, add the Modify Health Attribute script
- When the red spaceship hits the asteroid, the health of the red spaceship will decrease by -1. You can change this value by adjusting the value of Modify Health Attribute (Script) > Health Change
Did you know?
- If you changed the Health Change value to a positive number, you could introduce objects that facilitate healing/recovery into the game
2. Define the Winning Conditions

Dragging the UserInterface prefab onto the Hierarchy will allow you to see a visual representation of the changes in a player’s health and define the winning conditions of the game
You’ll define and program the winning conditions of the game
- Display the changes in the player’s health, by adding a pre-made design by going to Project > Prefabs > UserInterface and drag that into the Hierarchy
- By default, the player wins the game if she/he manages to get 5 points. If you want to change that, you can go to Inspector > UI Script (Script) > and change the value of Score To Win
- Now, go to Hierarchy > select SpaceshipRed. Go to Inspector and change the Tag from Untagged to Player
- Test the game by clicking play. When the player hits the asteroid, you will see that health drops by one point. When the points reach zero, the Game Over text appears

Tag the SpaceshipRed to allow the script to properly identify it as a player
Definitions
- Tagging GameObjects: allows scripts to classify, identify, and differentiate GameObjects