I have a health script which is not working for all assigned objects, I have 15 objects in a scene where I can kill 12 of the objects with this health script, and the 3 that are left are indestructible.
any idea how to fix it?
I have experienced the same problem with different amounts of objects…
using UnityEngine;
using System.Collections;
public class HealthController : MonoBehaviour {
public float startingHealth = 100.0f; // The amount of health to start with
public float maxHealth; // The maximum amount of health
private float currentHealth; // The current ammount of health
public float XPtoGive;
public float ScoretoGive;
public float MoneyoGive;
public bool replaceWhenDead = false; // Whether or not a dead replacement should be instantiated. (Useful for breaking/shattering/exploding effects)
public GameObject deadReplacement; // The prefab to instantiate when this GameObject dies
public bool makeExplosion = false; // Whether or not an explosion prefab should be instantiated
public GameObject explosion; // The explosion prefab to be instantiated
private bool dead = false; // Used to make sure the Die() function isn't called twice
public bool Enemy = false;
// Use this for initialization
void Start()
{
maxHealth = startingHealth;
currentHealth = startingHealth;
if (Enemy = false) {
XPtoGive = 0;
ScoretoGive = 0;
MoneyoGive = 0;
}
}
public void ChangeHealth(float amount)
{
currentHealth += amount;
if (currentHealth <= 0 && !dead)
Die();
else if (currentHealth > maxHealth)
currentHealth = maxHealth;
}
public void Die()
{
// This GameObject is officially dead. This is used to make sure the Die() function isn't called again
dead = true;
// Make death effects
if (replaceWhenDead)
Instantiate(deadReplacement, transform.position, transform.rotation);
if (makeExplosion)
Instantiate(explosion, transform.position, transform.rotation);
// Remove this GameObject from the scene and gives XP to player
if (Enemy = true) {
GameObject.FindGameObjectWithTag ("Player").GetComponent<PlayerController>().ChangeXP (+XPtoGive);
//GameObject.FindGameObjectWithTag ("Player").GetComponent<PlayerController>().ChangeScore(+ScoretoGive);
}
Destroy(gameObject);
}
}