Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
4
Question by 3dDude · Dec 27, 2010 at 09:33 PM · editorinstantiateprefabbce0005

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

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tetrad · Dec 27, 2010 at 10:35 PM 0
Share

"Got a few errors" What errors are you getting?

avatar image Bunny83 · Dec 28, 2010 at 04:23 AM 0
Share

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

avatar image 3dDude · Dec 28, 2010 at 02:41 PM 0
Share

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.

5 Replies

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JoeStrout · Sep 05, 2014 at 01:42 PM 0
Share

Fantastic! I didn't know about ScriptableWizard -- that's really handy!

avatar image Roiw · Mar 12, 2018 at 04:56 PM 0
Share

Thanks! Nice answer!

avatar image
0

Answer by LunHai 1 · Dec 28, 2010 at 03:28 AM

var thePrefab : Transform;

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image 3dDude · Dec 28, 2010 at 03:45 AM 0
Share

does not work.. thanks for trying though!

avatar image NicRule · Jun 24, 2015 at 01:26 AM 0
Share

you must include using UnityEditor; in the script to use PrefabUtility.InstantiatePrefab(GO)

avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image 3dDude · Dec 30, 2010 at 10:04 PM 0
Share

Ok thanks! could you recommend a method that works on a non editor script?

avatar image femi · Dec 30, 2010 at 11:06 PM 0
Share

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.

avatar image
0

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;
     }
     
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Prefabs are instantiated in editor but not in executable 2 Answers

Why my prefab is auto changing? 3 Answers

Instantiating Prefab from Javascript - BCE0005: Unknown identifier: 'Prefab' 2 Answers

Editor Script: Linking GameObjects to public script variables resets when playing. 1 Answer

How can I create a grid for a Leveleditor? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges