Passing an Int

Hey!

I have a Player Health Bar, displaying a different amount of “green” line, depending on what my Players Health is - in a standalone script, my players health bar, works perfectly - unfortunately, when trying to access the players health from my Player Controller script, the int is read as “0” when it should be 10.

Here is my entire Player Health Bar Script

using UnityEngine;
using System.Collections;

    public class playerHPS : MonoBehaviour {
    public float playerHP;
    public float maxHealth;
    public float curHealth;
    public int myPlayerHealth;
    public GameObject healthBar;

	// Use this for initialization
	void Start () {
        GameObject playerPos = GameObject.Find("playerPos");
        myPlayerHealth = playerPos.GetComponent<playerController>().playerHealth;
        maxHealth = 10;
        curHealth = myPlayerHealth;
	
	}
	
	// Update is called once per frame
	void Update () 
    {
        curHealth = myPlayerHealth;
        float pointHealth = curHealth / maxHealth;
        SetHealthBar(pointHealth);
        Debug.Log(curHealth);
        Debug.Log(myPlayerHealth);
	}

   public void SetHealthBar (float curHealthVisual)
{
        healthBar.transform.localScale = new Vector3 (Mathf.Clamp(curHealthVisual, 0f, 1f), healthBar.transform.localScale.y, healthBar.transform.localScale.z);
       
}
}

Note: Debug.Log verifies this numeral is 10 @ game launch, and changes every time my player is hit to the correct amount on my other script (playerControler), So what I understand is somehow the value is different in each script, although it should be pulling it directly off the first one.

Can anyone please help?

myPlayerHealth is not being updated to equal the players health, as you are not referencing the player component in the Update function. We should store the reference to the player so that we can get it’s health inside of the Update function, without calling GetComponent every frame. Below are the changes that I would make:

using UnityEngine;
 using System.Collections;
 
     public class playerHPS : MonoBehaviour {
     public float playerHP;
     public float maxHealth;
     public float curHealth;
     public GameObject healthBar;
     public playerController myPlayer;

 
     // Use this for initialization
     void Start () {
         GameObject playerPos = GameObject.Find("playerPos");
         myPlayer = playerPos.GetComponent<playerController>();
         maxHealth = 10;
         curHealth = myPlayer.playerHealth;
     
     }
     
     // Update is called once per frame
     void Update () 
     {
         curHealth = myPlayer.playerHealth;
         float pointHealth = curHealth / maxHealth;
         SetHealthBar(pointHealth);
         Debug.Log(curHealth);
     }
 
    public void SetHealthBar (float curHealthVisual)
 {
         healthBar.transform.localScale = new Vector3 (Mathf.Clamp(curHealthVisual, 0f, 1f), healthBar.transform.localScale.y, healthBar.transform.localScale.z);
        
 }
 }

This will change the health bar, as long as the playerHealth is changed at some point (EX: Taking damage). Obviously, the overall structure of your code needs some work, but you’ll learn that stuff with experience.