Im sure there is an easy way to do this , but the docs are appraently wrong for Object.FindObjectsOfType in c#.
Anwyay, ive instantaited a lod of prefabs into the main heirarchy and now i want to destroy them all. How can I get a list of only these clones and call destroy for them? (i tried a loop to find all objects of named type, but unless you use destroy immediate they dont actually go until update).
Thanks
Have you thought about maintaining a list manually? It would be faster than trying to find them in the world, and less error prone.
public class PrefabManager {
List<PrefabClass> _activePrefabs = new List<PrefabClass>();
void PrefabSpawned(PrefabClass _newPrefab){
}
void DestroyAllPrefabs(){
foreach (PrefabClass in _activePrefabs){
Destroy(_activePrefab.gameObject);
}
}
}
public class PrefabClass{
void Start(){
PrefabManager.Instance.PrefabSpawned(this);
}
}
You could use an enum or something to make a public enum PrefabTypes{ } - or you could still just use gameObject.Name if you ever wanted to destroy only a single type of prefab but maintain all active prefabs in a single list. Alternatively you could always use a Dictionary<string/enum PrefabType, List> to keep track of them all in a single organized location if you wanted.
Yeah, that’s a nice idea, I might do that for another aspect of the application.
What I ended up doing was creating an empty gameobject on the heirarchy , adding all my spawned prefabs to it as children and then deleting the parent when i wanted to get rid of them all. Works like a dream!
The only thing you have to remember, is that adding these objects to the heirarchy itself will also set their local rotations and positions to the parent object in the heirarchy.
Another thing you could do, is create a c# singleton manager like so:
public class Derp : GameObject
{
public static ArrayList instances = new ArrayList();
public GameObject()
{
Derp.addInstance(this);
}
public static void addInstance(Derp adding)
{
instances.Add(adding);
}
}
This will keep track of everything for you, and should work like a charm. Since it’s static, you just have the one.