Script not working for all assigned objects

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)
		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);

First of all, this line of code in Die():

if (Enemy = true) {

Assigns true to the variable Enemy, it doesn’t check the value of the Enemy variable. What you want is:

if(Enemy == true) {

or, if you want to write code that doesn’t give other programmers cancer:

if(Enemy) {

which does the exact same thing. You never need to write ‘x == true’ or ‘x == false’. It’s super redundant. Also, variables should start with small letters to differentiate them from methods and classes, so it should be ‘enemy’, not ‘Enemy’.

You have the same bug in Start() - it should be:


instead of

if(Enemy = false)

Now, that’s probably not what’s preventing your enemies from dying - the Destroy(gameObject) line should be called anyway. So, the Die() method is probably not being run - meaning that either ChangeHealth isn’t running, or isn’t getting the correct values, or whatever. Without knowing how the enemies take damage, I can’t figure that out for you, but you can figure it out yourself. Here’s what you do to debug:

Select the objects that are not dying in your scene. Go to the upper right of the inspector, and click the menu next to the lock symbol. Select “debug” from the drop-down.

Now you can see the state of the private variables on your objects. In particular, you should check if the “indestructible” objects are actually losing health, and if they are, what happens with the dead-variable when they die.

I’m guessing that the problem is probably that the enemies needs to have some kind of collider for whatever weapon you have to be hitting them, but you have forgotten to add it. Something along those lines.