- Home /
Find prefab from Instantiation
Hi everybody, I want to know if it's possible to find the prefab location of a Instantiate mesh in my scene. I know it's possible to instantiate a prefab in that scene but I want to do the inverse. I got my mesh/prefab instantiate and I want to know where is the object use to instantiate in my Project hierachy.
Is it possible to do that ?
Answer by Wolfram · Jun 21, 2012 at 02:53 PM
PrefabUtility.GetPrefabParent(PrefabUtility.FindPrefabRoot(myInstance))
To get the actual asset path, use AssetDatabase.GetAssetPath() on the returned object.
Thanks :) I just see the person who did the instantiation use not only prefab but meshes too. You think that can work on them too ?
You mean models? Yes, they are also recognized as Prefabs. Although their type will be different (see PrefabUtility.GetPrefabType and here).
If you're actually talking about the $$anonymous$$esh
object itself - FindPrefabRoot can only handle GameObjects, so you would have to get the GameObject your mesh is part of first.
Answer by TheDavil86 · Jun 21, 2012 at 01:04 PM
Not sure what you're trying to do or I could help better. My guess is you want to run a check to see what an object is. I don't know of any code for that, but you could add a public variable to the objects you want to check and look at that to determine it. For example:
// For a vegetable
public var objecttype = 1;
// For a mineral
public var objecttype = 2;
// check what the object is
if (gameObject.objecttype == 1) {
// Spawn a new vegetable
Instantiate(vegetable);
}
else if (gameObject.objecttype == 2) {
// Spawn a new mineral
Instantiate(mineral);
}
Hopefully that gives you an idea.
I think I explain it wrong :/. So i've got a script which instantiate an object. And at runtime, if I've got a mesh, I want to know where is the prefab which allow me to create this mesh. I want to know where is its location in the project (path or something) just with the name or the reference of the instantiate object.
Please don't post comments as answers, use the "add new comment" button.
Your answer
Follow this Question
Related Questions
Instantiate a Prefab along the surface of a Mesh 2 Answers
Combining Meshes? 1 Answer
Changing game object based on variable state? 1 Answer
Calling a function from a PreFab 1 Answer
Layout Group not working when instantiate from prefab 1 Answer