- Home /
 
I can't give damage with these scripts
this is my enemy health script
  int maxEnemyHealth = 100;
 public float currentEnemyHealth;
 internal bool gotDamage;
 GiveDamageToEnemy giveDamege;
 
 // Use this for initialization
 void Start () {
     giveDamege = GetComponent<GiveDamageToEnemy>();
     currentEnemyHealth = maxEnemyHealth;
 }
 
 // Update is called once per frame
 void Update () {
     if (gotDamage)
     {
         currentEnemyHealth -= giveDamege.damage;
         gotDamage = false; 
     }
        if(currentEnemyHealth <= 0)
     {
         Debug.Log("Ouch");
     }  
         
 
               and this is my giveDamageToEnemy script
public float damage; EnemyHealth enemyHealth;
 void Start()
 {
     enemyHealth = GetComponent < EnemyHealth > ();
     
 }
  void OnTriggerEnter(Collider2D other)
 {
     if (other.tag == "PlayerItem")
     {
         enemyHealth.gotDamage = true;
     }
 }
 
               }
when I save these scripts and return to unity I see ''ouch'' notification but when I press play and hit the enemy I can't see any notification and the enemy's not losing any hp. Can anyone help me with this?
Answer by KittenSnipes · Jan 28, 2018 at 08:16 AM
I think you should set up your scripts a bit differently because it is cleaner that way. I do not mean to discourage you but here is a setup that should work:
Health Script For Any Object This Is Attached This To:
     //The max health of the creature 
     //you are adding the script to.
     public float maxHealth = 100;
 
     //The health the creature currently has.
     private float currentHealth;
     void Start () {
         //On start set it's health to full health.
         currentHealth = maxHealth;
     }
 
     void TakeHealth(float DamageAmount)
     {
         //If you want this to add health then 
         //set the damage value as a negative number.
         currentHealth -= DamageAmount;
     }
 
               Here is the script you would use to damage your Object:
 (Insert Name Of Health Script Here) enemyHealth;
 public float DamageAmount;
 void Start()
  {
      enemyHealth = GetComponent <(Insert Name Of Health Script Here)> ();
      
  }
   void OnTriggerEnter(Collider2D other)
  {
      if (other.tag == "PlayerItem")
      {
          enemyHealth.TakeDamage(DamageAmount);
      }
  }
 
               Feel free to comment if there is any problems with the scripts.
Answer by LAZYGAMES_ · Jan 28, 2018 at 08:25 AM
In your enemy health script you are using the line:
  currentEnemyHealth -= giveDamege.damage;
 
               That means that you reduce the current health with damage. This damage is a float or integer in your giveDamageToEnemy script as you declared giveDamege like that:
 giveDamege = GetComponent<GiveDamageToEnemy>();
 
               The scripts should work if you add this float or integer damage in your giveDamageToEnemy script and set it to a value >0. This value is the damage the enemy is going to take per hit.
 Hope that helped.
well, I solved the problem with $$anonymous$$ittenSnipes's help but thanks for your answer.
Your answer