So we just created a damage system, let’s put it to use by now by shooting our zombies. We have a shoot script, and we know what we are shooting thanks to our raycast hit, however if we were to use the name from the raycast, we’de be getting names like “Enemy”, “Enemy 01”, “Enemy 02”. Nobody’s got time to check for different names though, so let’s take advantage of Unitys tag system and give our Enemy prefab an Enemy tag:
Now we can have an unlimited amount of enemies and all we need to do is check its tag, so let’s do just that!
In our Shoot scripts FireGun() method:
While the hit holds the object, in order to access the tag, we’ll have to specify transform first. So if that transform.tag is the Enemy tag, then we’ll get a debug log of it, let’s check!
It appears to be working! Now let’s access the damage function, first we’ll need to create a holder for it.
now that we have it, let’s first create a null check, and then if it’s successful, we’ll access the Damage() function and pass 50 health into it, essentially giving the zombie 3 hits.
Great! Now let’s test it!
WooHoo! We have depleting health and destruction when it reaches the minimum! I’m going to give myself a pat on the back, this mission is accomplished!