Find prefab path of a gameObject?


Does anyone know how I could find the prefab path of a gameobject?

Something like (go is an ordinary gameObject):

string path = findPrefabPath(go);
GameObject newGO = GameObject.Instantiate(Resources.Load(path));

I know this code sample here does not make so much sense, but I really need this function as it would be used in the sample above.

I found this: but it is not all I need, this would only give the information if an object has some kind of prefab connected to it. I need the path it is connected to (or even disconnected from).

Is it possible?

Thanks in advance!

try this

Object parentObject = EditorUtility.GetPrefabParent(obj); string path = AssetDatabase.GetAssetPath(parentObject); Debug.Log("prefab path:" + path);


You would use the AssetDatabase class. Check the scripting ref, there’s a lot of good stuff in there. IE:

string myPath = AssetDatabase.GetAssetPath( myGameObjectOrPrefab );

If you need find object in Prefabs, Material, Scene, by GUID

Click Right-click select “Find GUID in Prefabs Material Scene” the object to find in Prefabs, Material, Scene, by GUID.

Unless you need the exact prefab setups, you can always instantiate an object from a GameObject (prefabs are just that) from the scene :

GameObject newGO = GameObject.Instantiate(go);

Better use this

string path = AssetDatabase.GetAssetPath(prefabParent);

GameObject clone = null;
string path = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(Selection.activeGameObject);
var prefab = AssetDatabase.LoadAssetAtPath(path, typeof(GameObject));

if (prefab != null)
      clone = PrefabUtility.InstantiatePrefab(prefab) as GameObject;
      clone = Instantiate(Selection.activeGameObject);

Here’s how I got it working.