- Home /
Editor script instantiating prefab and maintaining link?
I'm trying to make a quick editor script that replaces the children of a selected object with a prefab.
How can I link the instantiated objects to the original prefab?
Answer by qJake · Jul 07, 2010 at 07:45 PM
Read the documentation for the Prefab functions in the EditorUtility
class:
http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=editorutility+prefab
You should be able to figure something out to solve your problem.
edit
Unity moved the InstantiatePrefab method into a seperate class some time ago. You have to use PrefabUtility.InstantiatePrefab now
It isn't good information anymore either. See this answer from @Nuthael ins$$anonymous$$d.
I updated this answer again for the new (Nested) Prefab System in Unity
Answer by Nuthael · Jul 27, 2012 at 01:45 AM
Had the same problem and just found the answer:
Use PrefabUtility.InstantiatePrefab() to instantiate the objects.
you must include using UnityEditor; in the script to use InstantiatePrefab.
This should be set to the Best Answer. Still holds up 6 years later.
The PrefabUtility class did not exist back then. It was later added. The accepted answer is correct for the time the question was asked. Yet just based on the upvotes and on the answer dates it should be clear that something has changed. This happens all the time and we do not and can not change all questions that were ever asked here each time Unity updates something. Accepting an answer is the duty of the person who asked the question.
Was just a statement.... In the same way "Today is a prettier day than yesterday", is a statement. I don't expect anything out of it.
It would be helpful it there was an actual example of how to do it..
It's up to you how you want to use this. I personally call it in a menu script that let's me automatically setup a scene with necessary components with 1 click.
using UnityEditor;
[InitializeOnLoad]
static class LoadEditorPrefab
{
public static void LoadPlayerPrefab()
{
//Give the path of the object to load and cache it in a variable
Object playerPrefab = AssetDatabase.LoadAssetAtPath("Assets/Player.prefab", typeof(GameObject));
//Instantiate prefab if it exists
if (playerPrefab != null)
{
PrefabUtility.InstantiatePrefab(playerPrefab);
}
}
}
Answer by IvovdMarel · Jan 02, 2020 at 06:05 AM
Just an updated answer on this, as Unity updated their prefab system:
//Get path to nearest (in case of nested) prefab from this gameObject in the scene
string prefabPath = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(gameObject);
//Get prefab object from path
Object prefab = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(Object));
//Instantiate the prefab in the scene, as a sibling of current gameObject
PrefabUtility.InstantiatePrefab(prefab, transform.parent);
Answer by jjxtra · May 18, 2019 at 03:02 PM
PrefabUtility.InstantiatePrefab returns null when I pass it a prefab from AssetDatabase.LoadAssetAtPath. Not sure if this is a recent bug, but happens in Unity 2017.4 or newer
Your answer
![](https://koobas.hobune.stream/wayback/20220613064105im_/https://answers.unity.com/themes/thub/images/avi.jpg)