Taking damage is not working

Hi there, I created two scripts, the first to assign to the unit that takes the damage and the other to the unit that attacks.

The problem is the object doesn’t get destroyed or harmed in any way.
The collider is set up correctly because I use it also for other things.

using UnityEngine;

public class PlayerHealth : MonoBehaviour
{

  public int startingHealth = 100;                            // The amount of health the player starts the game with.
  public int currentHealth;
  bool isDead = false;                                                // Whether the player is dead.
  bool damaged;

  void Awake ()
{
    // Set the initial health of the player.
    currentHealth = startingHealth;
}

    public void TakeDamage (int amount)
    {
        // Set the damaged flag so the screen will flash.
        damaged = true;

        // Reduce the current health by the damage amount.
        currentHealth -= amount;

        // If the player has lost all it's health and the death flag hasn't been set yet...
        if(currentHealth <= 0 && !isDead)
        {
            // ... it should die.
            Death ();
        }
    }


    void Death ()
    {
        // Set the death flag so this function won't be called again.
        isDead = true;
        Destroy(gameObject);
        Debug.Log("FIREWALL IS DEAD");
    }
}

the second script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Attack : MonoBehaviour
{
  public int attackDamage = 10;               // The amount of health taken away per attack.
  public bool time2Fight;

     GameObject player;                          // Reference to the player GameObject.
     PlayerHealth playerHealth;                  // Reference to the player's health.

     void Awake ()
     {
         // Setting up the references.
         player = GameObject.FindGameObjectWithTag ("Player");
         playerHealth = player.GetComponent <PlayerHealth> ();
     }

     void Loop()
     {
       if(time2Fight == true)
       {
         Fight();
       }
     }

     void OnTriggerEnter2D(Collider2D Player)
         {
           time2Fight = true;
         }


    void Fight ()
    {

      // If the player has health to lose...
        if(playerHealth.currentHealth > 0)
        {
            // ... damage the player.
            playerHealth.TakeDamage (attackDamage);
        }
    }

}

I don’t see anything that invokes the Loop() function. Have you tried adding a Debug.Log statement to ensure it’s actually running?

2 Likes

I’ll take that a step further… when stuff doesn’t seem to do anything, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.

2 Likes