Referencing playerHealth (from a separate script) in an if statement
So I have a playerController and hudController. Within the hudController I have a health system with a field called "currentHealth"
within the playerController I have the following code:
hudController playersHealth;
void Start()
{
playersHealth = GetComponent<hudController>();
}
void stateManagement()
{
if (playersHealth.currentHealth == 0)
{
state = State.Dying;
playAnim.SetTrigger("isDead");
Invoke("ReloadLevel", 2f);
}
}
Am I missing something obvious here? as far as I can tell this should be working...
Quick addition, the hudController health variable works perfectly, whenever I am marked as "hit" my health lowers by 1. The code is below.
public float maxHealth = 10; public float currentHealth;
void Start()
{
currentHealth = maxHealth;
}
private void OnTriggerEnter(Collider other)
{
if (currentHealth < 10 && other.tag == "health") { Debug.Log ("Success"); } //will be to update amount of hearts showing on HUD
}
public void addDamage(float damage)
{
if(currentHealth > 0) { currentHealth -= damage; }
}
Answer by Ak0rn · May 02, 2019 at 08:06 PM
I figured it out, was a f*cking moron and didn't put my player reference into my getcomponent
Change playersHealth = GetComponent to thePlayer.GetComponent
with "thePlayer" meaning find gameobject with tag "player".