- Home /
How to use EditorUtility.InstantiatePrefab?
Bump
Bump again
Hello,
the problem is when instantiating prefabs in the editor, The prefab connection is lost.
I have found a few people here that are asking similar questions. But none of them have answers that have helped me that much.
In one of the questions someone said to use EditorUtility.InstantiatePrefab.
now my question is how to get it working right.
I tried using it in place of Instantiate but Got a few errors. The docs don't say anything on the matter so I thought I would ask here.
Problems:
1# does a script using this function need to be in the editor folder?
2# does it have the same variables as Instantiate I.G gameObject, position, rotation?
thanks.
Edit:
Ok the error I got was:
Assets/InstantiateTests.js(4,9): BCE0005: Unknown identifier: 'InstantiatePrefab'.
this is not the exact code but It gives the same error:
var thePrefab : Transform;
function theFunction () { InstantiatePrefab(thePrefab,transform.position,transform.rotation); }
I'm not sure what exactly you want to do. Do you want to instantiate an object in within an Editorscript, or what do you mean by "instantiating in the editor". Or do you instantiate your object via the editor (you drag your prefab into the scene) and then lost the prefab connection?
The EditorUtility stuff only works within the editor and therefore have to be placed in the editor folder.
An object lost his connection to the prefab when you change the "content" of the object like adding or removing child GameObjects or components but that's normal.
Some more information plz ;)
Ok sorry, the script I showed up top is not the real script but has the same errors, the script is NOT in the editor folder. I am using a C# editor script that creates a editor button that you can click and it calls theFunction.
Answer by s4vi0r · Dec 29, 2010 at 03:16 PM
The properites of your prefab depend on what Type it is. If it is a Gameobject prefab. You will have access to all normal Gameobject members and functions. In your example your prefab is of type transform so you should be able to access things like rotation position etc. with no problem.
See if this helps: Make a script and put this code in it. Then attach it to an empty gameobject in the world.
var prefab : GameObject;
function MakePrefab() { var myPrefab = Instantiate (prefab, Vector3(0,0,0), Quaternion.identity);
//to access info about the prefab...scripts, postion, etc. use what ever you assign it to.. in this case "myPrefab" myPrefab.GetComponent("scriptName").SetMyValue(5); myPrefab.transform.position = Vector3(5,5,5); }
Drag and drop a prefab that you have created from your asset folder to the open slot in of the script component attached to that empty gameobject you created in the inspector window.(will be beside the word "prefab", it will say missing).
Hope this helps.
Answer by catofong · Jul 18, 2013 at 07:03 PM
I finally figured this out. Hope this helps others. This is for Unity 4.
For those looking to preserve the prefab link when you construct a GameObject in the scene:
1) Create a script under Assets/Editor so you can call PrefabUtility.
2) Drag a prefab from the "Project" into the inspector variable "go" below.
3) Click "Create" to create a prefab with the linkage in place. Also name and position changed through script.
using UnityEngine;
using UnityEditor;
public class DuplicatePrefab : ScriptableWizard
{
[MenuItem("GameObject/Create Other/Duplicate Prefab...")]
static void CreateWizard()
{
ScriptableWizard.DisplayWizard("Duplicate Prefab",typeof(DuplicatePrefab));
}
void OnWizardUpdate()
{
}
UnityEngine.Object duplicatePrefab( UnityEngine.GameObject go )
{
// FYI: Don't need to call this if go is already a prefab:
//UnityEngine.Object prefab = UnityEditor.PrefabUtility.GetPrefabObject( go );
return UnityEditor.PrefabUtility.InstantiatePrefab( go );
}
void OnWizardCreate()
{
UnityEngine.Object prefab = duplicatePrefab( go );
UnityEngine.GameObject dupGO = (UnityEngine.GameObject)prefab;
dupGO.name = "new name";
dupGO.transform.position = new Vector3(1,1,1);
}
public UnityEngine.GameObject go;
}
Fantastic! I didn't know about ScriptableWizard -- that's really handy!
Answer by LunHai 1 · Dec 28, 2010 at 03:28 AM
var thePrefab : Transform;
function theFunction () {
gameObject.InstantiatePrefab(thePrefab,transform.position,transform.rotation); }
you must include using UnityEditor; in the script to use PrefabUtility.InstantiatePrefab(GO)
Answer by femi · Dec 30, 2010 at 10:00 PM
The error "Unknown identifier: 'InstantiatePrefab'" is because you didn't specify the class function belongs to, try:
var thePrefab : Transform;
function theFunction () { EditorUtility.InstantiatePrefab(thePrefab,transform.position,transform.rotation); }
And the documentation of EditorUtility
does say:
Note: This is an editor class. To use it you have to place your script in Assets/Editor inside your project folder. Editor classes are in the UnityEditor namespace so for C# scripts you need to add "using UnityEditor;" at the beginning of the script.
So you won't be able to do that in your game scripts.
Ok thanks! could you recommend a method that works on a non editor script?
What exactly are you trying to accomplish? If you want to create copies of a prefab at run time, why do you care about losing the prefab connection? At run time you should be doing what s4vi0r sais.
Answer by JoeStrout · Sep 05, 2014 at 01:43 PM
I made a couple of tweaks to catofong's example; this shows how you can add additional controls to your wizard, used here to auto-select the newly created object; and how you can make the selected prefab stick between invocations, so you don't have to reselect it every time.
using UnityEngine;
using UnityEditor;
public class CloneWizard : ScriptableWizard
{
public GameObject prefab;
public bool selectIt = true;
static GameObject lastPrefab;
[MenuItem("GameObject/Create Other/Clone Wizard...")]
static void CreateWizard()
{
ScriptableWizard.DisplayWizard<TreeWizard>("Clone Wizard").prefab = lastPrefab;
}
void OnWizardUpdate()
{
if (prefab != null) lastPrefab = prefab;
}
void OnWizardCreate()
{
GameObject clone = UnityEditor.PrefabUtility.InstantiatePrefab(prefab) as GameObject;
clone.name = "new name";
clone.transform.position = new Vector3(1,1,1);
if (selectIt) Selection.activeGameObject = clone;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613092743im_/https://answers.unity.com/themes/thub/images/avi.jpg)