Search Unity

damage test not working?

Discussion in 'Scripting' started by Phobiegames, Sep 18, 2019.

  1. Phobiegames

    Phobiegames

    Joined:
    Apr 3, 2017
    Posts:
    113
    Code (CSharp):
    1.     public void DealDamage(int dmg, int hpTarget, int currentTargetHealth)
    2.     {
    3.         currentTargetHealth = hpTarget - dmg;
    4.     }
    5.  
    6.     public void PlayerAttack()
    7.     {
    8.         DealDamage(playerData[0].strStat, playerData[0].hpStat, playerData[0].hpStat);
    9.         //Debug.Log(playerData[0].hpStat);
    10.     }
    So not sure what i'm doing wrong, but while testing this, the player hp isnt dropping when i debug this. I have PlayerAttack() hooked up to a ui button.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Primitive values aren't passed by reference, but by value. So your third parameter in particular, it just passes in whatever the value of hpStart is, but that's not in any way linked back to the hpStat member.
    You probably should rewrite it to use a return value:
    Code (csharp):
    1. public int DealDamage(int dmg, int hpTraget) {
    2. return hpTarget - dmg;
    3. }
    4. public void PlayerAttack() {
    5. playerData[0].hpStat = DealDamage(playerData[0].strStat, playerData[0].hpStat);
    6. }
     
    Phobiegames likes this.
  3. Phobiegames

    Phobiegames

    Joined:
    Apr 3, 2017
    Posts:
    113
    OHHHH okay, thank you! I just learned something lol