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 =((((
}
bdjnk
May 14, 2013, 7:45am
3
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.
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