- Home /
Get all instances of a prefab
I'm aware of Object.FindObjectsOfType, but as far as I know, it only finds built-in types (Material, RigidBody, etc). If not, how can I make to find all objects instances of my owm prefab?
I'm also aware of GameObject.FindGameObjectsWithTag, but I'm trying to find an alternative.
If it helps, I want to find it during Edit Mode.
Thanks!
That would be very convenient, I remember wanting this too. I think I ended up using tags.
Or you could keep track of them yourself: Save a reference to your prefabs when you instantiate them. Instantiate returns such a reference, after all. In the script where you call Instantiate, just make a list and stick the call in List.Add. Then you can traverse that list later on, when you want access to those prefabs. Would that be possible for your scenario?
Christian: No, since Im creating them using EditorWindow, the references to the GameObjects are lost on entering Play $$anonymous$$ode.
can you write them to a file and get another script to load from that file at runtime?
Answer by Bunny83 · Oct 13, 2011 at 08:11 PM
Well, instances of prefabs have to be in the scene, so you will have to iterate through all objects and check them against your prefab reference. To get the prefab of an instance just use EditorUtility.GetPrefabParent() on your instance and you will get back a reference to the prefab asset.
This is the implementation of the "select" button inside the GameObjectInspector:
if (prefabType != PrefabType.MissingPrefabInstance && GUI.Button(position2, "Select", "MiniButtonLeft"))
{
Selection.activeObject = EditorUtility.GetPrefabParent(base.target);
EditorGUIUtility.PingObject(Selection.activeObject);
}
To check if an object in the scene is a prefab instance use EditorUtility.GetPrefabType().
Something like this should do:
// C# (not tested!)
List<GameObject> FindAllPrefabInstances(UnityEngine.Object myPrefab)
{
List<GameObject> result = new List<GameObject>();
GameObject[] allObjects = (GameObject[])FindObjectsOfType(typeof(GameObject));
foreach(GameObject GO in allObjects)
{
if (EditorUtility.GetPrefabType(GO) == PrefabType.PrefabInstance)
{
UnityEngine.Object GO_prefab = EditorUtility.GetPrefabParent(GO);
if (myPrefab == GO_prefab)
result.Add(GO);
}
}
return result;
}
Note that findObjectsOf Type will find ALL objects, also nested objects so you might end up with more than you'd expect. You might use EditorUtility.FindPrefabRoot if the prefab has nested objects to sort out the children of a prefab and to find the true root prefab.
Also take a look at the PrefabType since there are more than one possibilities. Choose what you need.
Just a final hint: remember that until now prefabs are an editor-only feature. At runtime there is nothing like prefabs anymore. There are only scene objects and off-scene objects (our former prefabs, loaded from assets). Instantiate just clones objects. At runtime there is no relationship between the original object and the cloned one.
Even in the editor you have to use EditorUtility.InstantiatePrefab to make the editor to remember the prefab-connection.
Answer by DaveA · Oct 13, 2011 at 12:44 AM
You can find prefabs, but prefabs of a certain type, not so much. One way is to put a particular script in your prefab, then look for all objects that contain that script, if tags aren't cutting it for you.
this was what I was going to suggest, but i seriously couldn't recommend it over tags...other than that unless they all had the same name then he could use GameObject.Find
It seems he's trying to make some Editor script - and the Editor can somehow find all objects created from a prefab. How they do that?
Could he add them to some sort of data structure? A list or array etc.
As I said on a comment above, I can't use references because I'm using and EditorWindow, which loses reference of objects when going into Play $$anonymous$$ode.