Decrementing health

Our BeetleNPC script already detects collisions with the Cucumber Man, so we can simply update that script to deduct a point from the Cucumber Man's health when a collision is detected. We will do this by making a call to the ReduceHealth() method of the HealthManager script. Here are the steps:

  1. In the Project panel, select Assets | Custom Scripts and double-click the BeetleNPC script.
  2. Add the following statement in the class variables section of the script. This creates a variable we can use to reference the HealthManager class:
      public HealthManager _healthManager;
  1. Add the following two statements in the OnCollisionEnter() method after the first if statement:
      _healthManager = GameObject.Find       ("Health_Slider")

Get Getting Started with Unity 2018 - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.