Now we have collisions in our prototype and the enemies can be destroyed. We want to make our cube take damage if hit by the enemy. To make this happen we have to make our collision recognise our player has lives. This is done through script communication.
Iv added a variable to my player called lives and given it the standard 3. Within the enemy script I want to call a method within the player script that takes away from the variable _lives.
this was before I wanted to communicate with the player script. It just communicated via tag “player” so if the object was equal to it would destroy.
this will communicate with the player script and run the method damage. You can only communicate with another object through its transform. so with get component we can select the player then use the method “damage” which is:
So the private variable within this script is called within this method to simply take away 1 life and when it reaches less than 1, destroy this object. Here is the result: