- Home /
Destroy all GameObjects EXCEPT some...
I need to Destroy all GameObjects on current scene, EXCEPT Player and Camera classes. How can I do it?
var agoAllGameObjects:GameObject[] = FindObjectsOfType(GameObject);
for(var iGO:int = 0; iGO < agoAllGameObjects.length; iGO++)
{
agoAllGameObjects[iGO].GetType(); //always GameObject :(
agoAllGameObjects[iGO].GetType("Name"); //always GameObject :(
if(agoAllGameObjects[iGO].GetType() != Camera)
Destroy(agoAllGameObjects[iGO]); //Destroys EVERYTHING =((((
}
Answer by bdjnk · May 14, 2013 at 07:45 AM
All GameObjects are of type GameObject. What distinguishes them is various attributes and components. For example I could say
if(agoAllGameObjects[iGO].name != "Camera")
or perhaps
if(agoAllGameObjects[iGO].GetComponent(Camera) == null)
but GetType on a GameObject will always return GameObject, because that IS its type.
Answer by adrenak · Nov 01, 2012 at 03:36 PM
The agoAllGameObjects itself is an arary of GameObject, so its only obvious that .GetType will always be a GameObject. To destroy everything except the player and the camera, what you can do is check for tag or name. So, inside the for loop, have these statements instead of the above ones :
if(agoAllGameObjects[iGO].tag != "Player" && agoAllGameObjects[iGO].tag != "Camera"){
Destroy(agoAllGameObjects[iGO]);
}
The above line assume that the player carries a tag named "Player" and the camera(s) have a tag called "Camera". It should work properly.
Hope this helps.
-Vatsal
The solution is obvious with TAGs, I should fix the problem WITHOUT using any TAG!
dear how have find the solution. i also need help in this regard