Time for action – giving it a weakness
The easiest way to weaken our enemies is to give them some health that is reduced when they are shot, and to destroy them when they run out of health:
We start by creating a new script and naming it
Health
.This script is rather short and starts with a single variable. This variable will keep track of the remaining health of the tank. By setting the default value to
3
, the tank will be able to survive three hits before being destroyed.public int health = 3;
This script also contains only one function,
Hit
. As in the case of the targets, this function is called by theBroadcastMessage
function when the player shoots at it. The first line of the function reduceshealth
by one point. The next line checks to see ifhealth
is below zero. If it is, the tank is destroyed by calling theDestroy
function and passing it thegameObject
variable. We also give the player a handful of points.public void Hit() { health--; if(health <= 0) { Destroy(gameObject...