Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
10
Question by chief1234 · Jul 07, 2010 at 07:36 PM · instantiateprefabeditor-scripting

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?

Comment
Add comment
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

4 Replies

· Add your reply
  • Sort: 
avatar image
13
Best Answer

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

Comment
Add comment · Show 3 · 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 eskivor · Nov 09, 2017 at 11:42 AM 0
Share

link is dead

avatar image SandLantern eskivor · Sep 19, 2018 at 06:51 PM 0
Share

It isn't good information anymore either. See this answer from @Nuthael ins$$anonymous$$d.

avatar image IvovdMarel · Jan 02, 2020 at 06:06 AM 1
Share

I updated this answer again for the new (Nested) Prefab System in Unity

avatar image
39

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.

Comment
Add comment · Show 6 · 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 NicRule · Jun 24, 2015 at 01:25 AM 3
Share

you must include using UnityEditor; in the script to use InstantiatePrefab.

avatar image camerontynespnnl · Jun 21, 2018 at 12:18 AM 1
Share

This should be set to the Best Answer. Still holds up 6 years later.

avatar image Bunny83 camerontynespnnl · Jun 21, 2018 at 01:10 AM -1
Share

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.

avatar image camerontynespnnl Bunny83 · Jun 21, 2018 at 01:15 AM 0
Share

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.

avatar image mihaelfi · Oct 03, 2019 at 09:00 AM 0
Share

It would be helpful it there was an actual example of how to do it..

avatar image camerontynespnnl mihaelfi · Oct 03, 2019 at 02:33 PM 2
Share

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);
         }
     }
 }
avatar image
6

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);
Comment
Add comment · Show 1 · 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
0

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

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Editing Terrain prefabs directly from a custom editor window. Updates not applying to scene instances. 0 Answers

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

objectReference is null in prefab 0 Answers

Placing a road 0 Answers

Unity Frame Drop From Instantiating Prefab that has script 0 Answers


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