hello all, im trying to make a health system for my game, te players runs with this script:
public int maxHealth;
public int currentHealth;
void Start ()
{
currentHealth = maxHealth;
}
public void TakeDamage (int amount)
{
currentHealth -= amount;
}
and i have a lazer prefab, with a script that handles the colision with the players and calls for the “take damage function”
public int baseDamage;
GameObject player;
void Start()
{
player = GameObject.FindGameObjectWithTag("Player");
}
void OnTriggerEnter (Collider other)
{
if (other.gameObject == player)
{
HealthSystem.TakeDamage (baseDamage);
}
}
}
i tryied to make it static, but it didn’t help.
thanks in advance
From what I understand, you want to reference the player’s HealthSystem component.
Try this :
public int baseDamage;
GameObject player;
HealthSystem healthSystem;
void Start()
{
player = GameObject.FindGameObjectWithTag("Player");
healthSystem = player.GetComponent<HealthSystem>();
}
void OnTriggerEnter (Collider other)
{
if (other.gameObject == player)
{
healthSystem.TakeDamage (baseDamage);
}
}
}
i tryied that and didnt work, anyway after some more changes i finally make it work:
public int baseDamage;
GameObject disparo;
void OnTriggerEnter (Collider other)
{
if (other.tag == "Player")
{
HealthSystem healthRef = other.GetComponent<HealthSystem> ();
healthRef.TakeDamage (baseDamage);
Destroy (gameObject);
}
}
thank you @UnityCoach for pointing me in the right direction, that was really helpful.