When i call some variables to an other second script i have a problem

Hello everyone. My problem is as follows :

I have 3 Diferent Java scripts

First Sript name i just call is “PlayerHp”, Second Script Name is “PlayerSkill”, Third script Name PlayerStats

PlayerHp:

static var curHp = 80;
static var dieHp = 0;
static var maxHp = 100;

PlayerSkill:

var damageHp = 10;
private var curHp = PlayerHp.curHp;
private var dieHp = PlayerHp.dieHp;
private var maxHp = PlayerHp.maxHp;
 
    function Update() {
       if(Input.GetButton("Attack") && curHp > dieHp) {
         curHp = curHp - damageHp;
         Debug.Log ("Hp" + curHp);
    }

PlayerStats:

public var curHp = PlayerHp.curHp;
public var dieHp = PlayerHp.dieHp;
public var maxHp = PlayerHp.maxHp;

but when made some change by PlayerSkill (for example pressing Input.GetButton(“Attack”), then the stats made change the didn’t update to playerHp or PlayerStats. How can i fix that?

1 Answer

1

Instead of assigning new private variables for your player stats use the static variables in your PlayerSkill Update function instead.

     function Update() {
            if(Input.GetButton("Attack") && PlayerHp.curHp > PlayerHp.dieHp) {
                PlayerHp.curHp = PlayerHp.curHp - PlayerHp.damageHp;
                Debug.Log ("Hp" + PlayerHp.curHp);
            }
     }