Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
3
Question by DCoin · Apr 26, 2018 at 01:06 PM · instantiateprefabeditor-scriptinginstantiate prefab

How to copy GameObject and preserve Prefab connection and Component values from editor script

Hey I am trying to copy a GameObject in my scene and preserve the prefab connection while also preserving the values that were changed in the instance's components. So far I have tried these two methods:

     [MenuItem("Test/Test1")]
     public static void Test1()
     {
         Object prefabRoot = PrefabUtility.GetPrefabParent(Selection.activeGameObject);
         PrefabUtility.InstantiatePrefab(prefabRoot);
     }
 
     [MenuItem("Test/Test2")]
     public static void Test2()
     {
         GameObject go = Object.Instantiate(Selection.activeGameObject);
         var prefabParent = PrefabUtility.GetPrefabParent(Selection.activeGameObject);
         PrefabUtility.ConnectGameObjectToPrefab(go, prefabParent as GameObject);
     }

But both of them behave like they instantiate a completely new copy of the prefab instead of keeping values like name, location etc. and the values that were changed from the original prefab values in the instance's components.

Essentially I just want to replicate the behavior of ctrl-D after which i can apply my own changes to the GameObject

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

2 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by unity-marcus · Jul 06, 2020 at 03:54 PM

Not pretty but it works (tested in 2019.3).

             Selection.activeGameObject = someGameObjectToCopy;
             Unsupported.CopyGameObjectsToPasteboard();
             Unsupported.PasteGameObjectsFromPasteboard();
             var copy = Selection.activeGameObject;
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 Tortuap · Jan 21, 2021 at 10:07 AM 0
Share

Very nice solution. Not pretty indeed, but totally working.

avatar image Wappenull · Mar 23, 2021 at 04:03 AM 0
Share

Unity, WTF with this fishy class name? But hey it worked.

avatar image
1

Answer by Leight · Jun 24, 2018 at 01:46 AM

Hello. I had same issue and spent a lot of time on this. Following code allows you to duplicate prefab objects and keep overrides:

                     // *** Try get prefab of an object *** //
                     GameObject  obj         = null;
                     Object      prefab      = PrefabUtility.GetPrefabParent(objectPrototype);
 
                     // *** Instantiate obj *** //
                     bool objIsPrefab = prefab != null;
                     if(objIsPrefab) {
                         // *** Save overrides from original obj *** //
                         PropertyModification[]      mods        = PrefabUtility.GetPropertyModifications(objectPrototype);
                         // *** instantiate clean prefab *** //
                         Object                      spawnedObj  = PrefabUtility.InstantiatePrefab(prefab);
 
                         // *** Apply prefab overrides to a new instance *** //
                         PrefabUtility.SetPropertyModifications(spawnedObj, mods);
                         obj = (GameObject)spawnedObj;
                     } else {
                         obj = Instantiate(objectPrototype, wspacePos, Quaternion.identity, parentObj);
                     }



Please note that this method only handles properties and objects that are already a part of your original prefab. For instance, if you put a "cube" inside your prefab instance in scene and execute this code "cube" will not appear inside a new duplicated object. So you need to modify this code in order to handle this situation too.

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

185 People are following this question.

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

Related Questions

Get a Insantiate particle system follow a GameObject 1 Answer

How to Change the Variables of an Instantiated Object? 0 Answers

Im having an issue when Instantiate Prefab with specific rotation 2 Answers

Prefabs not instantiating after build 0 Answers

I need help destroying a clone on function. 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