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
0
Question by deliberative · Jul 22, 2020 at 05:11 PM · prefabsprefabutility

Cloning a GameObject with Linked Prefab Children

Hello,

Say we have three objects: A Prefab Asset (call it "A"), a regular GameObject in the scene ("B"), and a prefab instance of A that is a child of B ("C"):


Project
Prefab Asset A

Scene Hierarchy
GameObject B
- Prefab Instance C (connected to Prefab Asset A)


I would like to clone GameObject B, in such a way that its children (i.e. Prefab Instance C) maintain their connections to their prefab assets, as well as any overrides they may have. This is trivial in the editor (using Duplicate, or Copy & Paste), however I need to do this via scripting.

UnityEditor.PrefabUtility.InstantiatePrefab doesn't work, since it won't accept a non-asset as a parameter, so I can't pass it GameObject B. Object.Instantiate obviously won't work either since it breaks the prefab connections. I've looked through the rest of PrefabUtility's static methods and don't see anything else there that'd be helpful, but I'm hoping that I'm missing something. Does anyone know of a way to do this via scripting?

Much thanks in advance.

(Unity 2019.4)

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by smark12007 · Jul 22, 2020 at 06:16 PM

Use some tricks like UnityEditor.Unsupported.DuplicateGameObjectsUsingPasteboard?

 UnityEngine.Object recordSelected = UnityEditor.Selection.activeObject;
 UnityEditor.Selection.activeObject = GameObjectB;
 UnityEditor.Unsupported.DuplicateGameObjectsUsingPasteboard();
 UnityEditor.Selection.activeObject = recordSelected;


https://forum.unity.com/threads/no-function-to-ctrl-d-duplicate-through-an-editor-script.263891/

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 deliberative · Jul 22, 2020 at 11:42 PM 0
Share

Creative. It worked. One issue with this is that DuplicateGameObjectsUsingPasteboard returns void, and I needed a reference to the new object so I can do things with it (like re-parent it). But it seems to reliably add the new object to the end of the parent's child hierarchy, so using GetChild on the parent transform seems to be a viable way to get the new object. I'm using this for an editor tool I hope to use in several projects, so I don't love using an unsupported method, but at least for now, it gets the job done. I'll leave this open for the moment in case anyone has any other suggestions, then close it.

avatar image smark12007 deliberative · Jul 23, 2020 at 10:40 AM 0
Share

For things you want to return, perhaps the trick is simple too

 UnityEngine.Object[] recordSelectedObjects = UnityEditor.Selection.objects;
 UnityEditor.Selection.activeObject = GameObjectB;
 UnityEditor.Unsupported.DuplicateGameObjectsUsingPasteboard();
 UnityEngine.Object GameObjectC = UnityEditor.Selection.activeObject;
 UnityEditor.Selection.objects = recordSelectedObjects;
 return GameObjectC;

Since you don't prefer unsupported methods, I'll look for elegant way when I have time.

avatar image Darkgaze smark12007 · Apr 12 at 08:17 AM 0
Share

As the previous answer said: UnityEditor.Unsupported.DuplicateGameObjectsUsingPasteboard(); <-- Sometimes doesn't select anything afterwards (specifically with prefabs in some ocasions, no idea why). And so Selection.activeObject or Selection.activeGameObject returns null.

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

138 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

Related Questions

PrefabUtility.CreatePrefab fails if prefab already exists 1 Answer

use PrefabUtility after build,PrefabUtility use after bulid 0 Answers

Reparenting object in prefab breaks object 3 Answers

PrefabUtility.ApplyPrefabInstance throws error when there is Script that does not directly derive on MonoBehaviour 0 Answers

Instantiating a prefab via PrefabUtility.InstantiatePrefab causes prefab to lose all references on it's scripts. 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