Classes and script references

using UnityEngine;
using System.Collections;
public class PlayerHp
{
    public float playerhealth = 100;
    public void DamagePlayer(float amount)
    {
        playerhealth -= amount;
    }
}

I have this script attached to my player. I access this script using public PlayerHp PlayerHealth = new PlayerHp(); and then alter the value of playerhealth using PlayerHealth.DamagePlayer(Damage) in another script.

There is another script which accesses the value of playerhealth using PlayerHp health = new PlayerHp(); and then calling health.playerhealth to access its value.

The problem is, when I alter the value of playerhealth in the second script the change is not applied to the third one, how can I solve this issue?

Thanks in Advance.

Try this:

static float playerhealth =100;

You’re creating a second instance of the PlayerHp class. While static would technically work a better solution would be to do a GetComponent on whatever script contains your first PlayerHp instance and then get the health value from that.

I can’t be using get component across different scenes.

Thanks for the suggestion Fuzzing I do know about static variables, but they are considered a not too good a practice while coding, so I was trying to avoid using static variables.