My text-based Health isn't working

I’ve created these line of codes so the enemy script is meant to call the PlayerHealth script. Then the PlayerHealth script is meant to call the text which I’ve created on a canvas and I’ve also got a separate script on it called HealthPoints. I can’t seem to get them 3 to work and decrease my health bar, i know it’s sort of all over the place and but if i can get some help that would be nice. Thank you heaps!

EnemyAttack
PlayerHealth playerHealth;
bool playerInRange;
GameObject player;

public int attackDamage = 10;
public GameObject lightEnemy;

// Use this for initialization
void Awake () {

	//setting up the references.
	player = GameObject.FindGameObjectWithTag ("Player"); 
	playerHealth = player.GetComponent <PlayerHealth> (); 

}

void OnCollisionEnter (Collision collision) {

			//if the entering collider is the player... 
			if (collision.gameObject == player) { 

					//...the player is in range. 
					playerInRange = true;

			} 

	} 

void OnCollisionExit (Collision collision) { 

	//if the exiting collider is the player...
	if (collision.gameObject == player) { 

	//...the player is no longer in range. 
		playerInRange = false;

			}

	}

// Update is called once per frame
void Attack () {

	//if the player has health to lose... 
	if (playerHealth.currentHealth > 0) { 

	//...damage the player. 
	playerHealth.TakeDamage (attackDamage);
		player.GetComponent<PlayerHealth> (); 

			}

}

}

PlayerHealth
public GameObject player;
public int startingHealth = 50;
public int currentHealth;
bool damaged;
bool isDead;

public Text healthPoints; 

void Awake () {

			currentHealth = startingHealth;
	} 

// Update is called once per frame
public void TakeDamage (int amount) { 

	//reduce the current health by the damage amount.
	currentHealth -= amount; 

	//set the health point's value to the current health. 
	healthPoints.text = "" + currentHealth; 

	//if the player has lost all its health and death hasn't been called yet...
	if(currentHealth <= 0 && !isDead) {

		Death (); 

	} 

} 

void Death () {  

	//if the bool is true...
	if(isDead = true) { 

		//...then load level.
		Application.LoadLevel (3); 

	} 

} 

}

HealthPoints
public int maxHealth;
public static int currentPlayerHealth;

Text text;

// Use this for initialization
void Start () { 
	text = GetComponent<Text> (); 
	currentPlayerHealth = maxHealth;

} 

// Update is called once per frame
void Update () { 
	if (currentPlayerHealth <= 0) { 
		currentPlayerHealth = 0;

		} 

	text.text = "" + currentPlayerHealth; 

}

}

If your GUI is having trouble getting values, there is a function on int called ToString() that returns a string value of your int. So for places where you set text you should set it this way:

text.text = currentPlayerHealth.ToString();

I haven’t used this with floats or doubles but I don’t see why ToString() wouldn’t be available for those if you ever need them.

As for potential problems, you seem to have multiple scripts that do the same thing and they may be interfering with each other (i.e. HealthPoints and PlayerHealth). You should instead think about all the functions that correspond to player health and keep them in one script. I’d get rid of HealthPoints and consolidate whatever it is you’re trying to do to PlayerHealth:

public GameObject player;
public int startingHealth = 50;
public int currentHealth;
public Text text;
bool damaged;
bool isDead;

void Start()
{
	isDead = false;
	isDamaged = false;
	currentHealth = startingHealth;
}

void Update()
{
	// In your code you have a typo of only one ‘=‘. One means you’re assigning,
	// two means you’re comparing values. The only time you’d put one is when you
	// have another comparison sign along with it like below.
	// I’d put isDead check here for the same reason I state with text.text below.

	if(currentHealth <= 0)
	{
		isDead = true;
	}

	if(isDead == true)
	{
		//Can have Death() here or just
		Application.LoadLevel(3);
	}

	// You can do this every time update is called so it’s “precise”,
	// But you can also put it in your TakeDamage function.
	text.text = currentHealth.ToString();
}

public void TakeDamage(int amount)
{
	// Since isDead is only true when we hit 0 on currentHealth, we can use this
	// to double check whether we can take damage or not.
	if(isDead == false)
	{
		currentHealth -= amount;
	}
}

In your EnemyAttack script, things look ok but I have no idea why you’re calling player.GetComponent<PlayerHealth>(); It doesn’t look like it does anything. You already have a reference to PlayerHealth at the start with the PlayerHealth playerHealth variable. All you have to do is use it once here:

// Your EnemyAttack script summed up.
PlayerHealth playerHealth;
GameObject player;

void Start()
{
	player = GameObject.FindGameObjectWithTag(“Player”);
	playerHealth = player.GetComponent<PlayerHealth>();
}

// This is just a note for you…
void OnCollisionEnter(Collision collision)
{
	// gameObject already has a tag variable you can use.
	// You don’t need to use collision.gameObject == player.
	if(collision.gameObject.tag == “Player”)
	{
		playerInRange = true;
	}
}

void Attack()
{
	// Notice no if statement here because it’s already handled by the PlayerHealth
	// script.
	playerHealth.TakeDamage(attackDamage);
}

I have no idea why you broke everything up when you didn’t need to.