Search Unity

Interface int amount to be used in a script

Discussion in 'Scripting' started by Nathan1357, Apr 18, 2021.

  1. Nathan1357

    Nathan1357

    Joined:
    Apr 14, 2021
    Posts:
    19
    Hey guys, can someone help me with my code?
    Code (CSharp):
    1.   public void Damage(float damageAmount)
    2.     {
    3.         currentHealth -= damageAmount * Time.deltaTime;
    4.  
    5.         if (OnHealthChanged != null) OnHealthChanged(this, EventArgs.Empty);
    6.  
    7.         else if (currentHealth < minHealth)
    8.         {
    9.             Die();
    10.         }
    11.     }
    12.  
    13.     public void Damage(int amount)
    14.     {
    15.         // From interface
    16.     }
    The public void Damage(int amount) already have an amount which i set in another script. How can i use the amount to be the damageAmount without deleting the damageAmount variable? Thankyou
     
  2. I'm not sure I understand what you want to do, but if you're asking if you can call the other method with the float parameter from the second method with the int parameter, then the answer is yes, like this:
    Code (CSharp):
    1.     public void Damage(float damageAmount)
    2.     {
    3.         currentHealth -= damageAmount * Time.deltaTime;
    4.         if (OnHealthChanged != null) OnHealthChanged(this, EventArgs.Empty);
    5.         else if (currentHealth < minHealth)
    6.         {
    7.             Die();
    8.         }
    9.     }
    10.     public void Damage(int damageAmount)
    11.     {
    12.         Damage((float)damageAmount);
    13.     }
     
  3. Nathan1357

    Nathan1357

    Joined:
    Apr 14, 2021
    Posts:
    19
    I mean how can i use the damageAmount as the float 'amount' in the first method