Destroy objects by using tag, only some parts of object is destroyed.

Hi guys,

I’ve been using a clock script by TJB, it allowes me to create events at certain time of day, but I ran into problems the minute I tried to extend it with “my own” code. This is what I’m using atm.

var lastChange = 0.0;
var timeSpeed = 0.00;
var hour = 0;
var minutes = 0;
var Hour = "24";
var SpawnNight: Transform;


function Update () {
    if (Time.time - lastChange > timeSpeed) {
        minutes++;
        if (minutes == 6) {
            minutes = 0;
            hour++;
            if (hour == 24) {
                if (Hour == "25") {
                   
                } else {
                    newDay();
                }
            }
            if (hour == 24) {
                hour = 0;
            }
			}
			if (hour == 5) {
				if (hour == "6") {
                                   
                } else {
                    killEnemy();
                }
            }
        lastChange = Time.time;
    }
}

function OnGUI () {
    GUILayout.Label(hour.ToString() + ":" + minutes.ToString() + "0");
}

function newDay () {
// whatever happens on a new day

// Instantiates copies of prefab each 2 units apart from each other

var prefab : Transform;

for (var i : int = 0;i < 1; i++) {
Instantiate (SpawnNight);
}
}

function killEnemy() {
// whatever happens at 5 in the morning

	Destroy(GameObject.FindWithTag("Enemy"));
	}
}

What I’m trying to achieve is that at 5 o’ clock in the morning all objects tagged with “Enemy” is destroyed, but what happens is that only some BONES in the last Enemy spawned is removed. I’ve tried tagging every part of the gameobject, but nothing works. Does anyone know how to fix this?

Seems like in your case you have more than o one object tagged with “enemy” , but you are trying to find only one, so it isn’t predictable which one Unity will find first.

Either you have only one GameObject tagged “Enemy” or you have many enemies (or child transforms of your single enemy) tagged “Enemy”.

In the latter case you’ll have to use GameObject.FindObjectsWithTag(“Enemy”) , which returns an array of GameObjects ( or child transforms) where you can iterate through and destroy every single enemy GO (or child :).

HTH,


oxl

Thanks for the reply Oxl, much appreciated. You got me on the right track, a little help from you and this thread fixed my problem. I thought I had done a good job searching, but I guess not, sorry for that:)

Best