Damage function affecting all instances of object
I am trying to get a damage script working, as it stands I have a bullet that calls the "takeDamage" function on the object which it collides with. The trouble is that calling the function of any single instance of the object calls all of the instances and reduces the "health" variable of all instances of the object.
Bullet script:
public class bulletStats : MonoBehaviour {
public float damage;
private void OnTriggerEnter(Collider other)
{
if (other.transform.tag == "enemy")
{
other.GetComponent<enemyHealth>().takeDamage(damage);
Destroy(gameObject);
}
}
...
Health script:
public class enemyHealth : MonoBehaviour {
private float health;
// Use this for initialization
void Start () {
}
//damage the asteroid
public void takeDamage(float damage)
{
health = health - damage;
Debug.Log(health);
}
This should work fine, nothing wrong with the script. It might be because of some other script.
Answer by howdyzach · Jan 13, 2018 at 10:57 PM
did you ever figure out the solution to this problem? I'm running into a similar issue myself
Your answer
Follow this Question
Related Questions
Referencing another object's instance 0 Answers
How do I make the object apply damage on trigger? 0 Answers
Why do I keep getting this error? 1 Answer
How to make my gun do damge and destroy a object? 1 Answer
Huge Health and Damage Script Problem 0 Answers