- Home /
Exceptions to tag references?
Hello. I want to make a function that when activated it destroys all objects with a certain tag EXCEPT a few SPECIFIC objects that are currently in the scene, can I do this? (Yes, I know the obvious answer would be to change the tag for those objects, but that would mess things up, with the way I have my scripts and references). Thanks.
EDIT- This is the script I want to put Adams script into. But, when I do (like the script below it), I get errors. What is wrong?
var Bomb : GameObject;
var Player : Renderer; var Light : Light; var Timer : float = 3;
function OnTriggerEnter (other : Collider) { if (other.gameObject.CompareTag ("Enemy")) { Stats.Life -=1;
Player.enabled = false;
Light.enabled = false;
Invoke("LightsOn", Timer);
};
if (other.gameObject.CompareTag ("Mul")) {
Stats.Mul +=2;
Destroy (other.gameObject);
}
}
if( Input.GetButtonDown( "Fire1" ) ) {if (Stats.Bomb >= 1){ Instantiate(Bomb, transform.position, transform.rotation); Stats.Bomb-=1;}}
function LightsOn(){ Player.enabled = true; Light.enabled = true;}
This is my attempt to meld the scripts.
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); } } }
Do you already know the few specific objects, or are they deter$$anonymous$$ed at runtime?
Answer by Adam Rademacher · Nov 30, 2010 at 04:51 AM
You'll want to make a list, array, etc of references to the game objects you want to keep somehow. The safest way is probably in the inspector, though if some of the objects are instantiated, you'll want to add them to the list/array when they are added to the scene. Then run some code similar to this:
List<GameObject> savedObjects = new List<GameObject>();
foreach(GameObject go in GameObject.FindObjectsWithTag("DestroyMe")) { if(!savedObjects.Contains(go)) Destroy(go); }
Javascript functionality (tested now):
var savedObjects : GameObject[];
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); } } }
Call SaveObject and pass the game object to it when you instantiate an object or decide to save it. Don't call DeleteObjects() in Update as it is quite slow to FindGameObjectsWithTag every frame.
I will update my script portion to use javascript ins$$anonymous$$d.
Thanks. I have tried to meld that script with my existing script(as I did with my new edited question) I get errors, what am I doing wrong?
if( Input.GetButtonDown( "Fire1" ) ) {if (Stats.Bomb >= 1){ Instantiate(Bomb, transform.position, transform.rotation); Stats.Bomb-=1;}} needs to go into function Update() {}