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);
}

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.

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;
    

    }

var thePrefab : Transform;

function theFunction () {

gameObject.InstantiatePrefab(thePrefab,transform.position,transform.rotation);
}

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.

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;
	}
	
}