Hello. This is a script I am using for handling the collisions/deaths in my games. When I run it I get the error
How can I make it so that the script works. To put in context, when the object that this script is attached to collides with an object with the tag “Enemy” I want to destroy all objects with the tag “Enemy” EXCEPT for a handful of SPECIFIC objects.
var savedObjects : GameObject[];
function OnTriggerEnter (other : Collider) {
if (other.gameObject.CompareTag ("Enemy")) {
Stats.Life -=1;
Invoke("DeleteObjects");
}}
function SaveObject(go : GameObject)
{
var tempObjects : GameObject[];
tempObjects = new GameObject[savedObjects.Length + 1];
Debug.Log(go.name);
if(savedObjects.Length > 0)
{
for(i = 0; i < savedObjects.Length; i ++)
{
tempObjects[i] = savedObjects[i];
}
}
tempObjects[tempObjects.Length - 1] = go;
savedObjects = new GameObject[tempObjects.Length];
for(i = 0; i < tempObjects.Length; i++)
{
savedObjects[i] = tempObjects[i];
}
}
function ArrayContains(array : GameObject[], item : GameObject) : boolean
{
for(i = 0; i < array.Length; i ++)
{
if(array[i] == item)
{
return true;
}
}
return false;
}
function DeleteObjects()
{
for(var go in GameObject.FindGameObjectsWithTag("destroy"))
{
if(!ArrayContains(savedObjects, go))
{
Destroy(go);
}
}
}