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 /
avatar image
1
Question by instruct9r · Dec 07, 2015 at 12:59 AM · c#gameobjectcomponentcopypaste

What happens when i Copy an object (From Programming point of view)

Hello.

Pure curiosity... I wonder, what actually happens, when i go to Edit > Copy, while selecting a gameObject (In the Hierarchy), under the hood?

I see that all of the referencing, that the Components of the copied object are kept, even if i delete it and then paste it in the scene....

•Does Unity creates something like a virtual instance of that object?

• Does Unity goes through all of the properties of all components and records their state, at the moment of copy?

1: Can i replicate the copy via C# in unity (Without doing any DLL stuff)?

I was wondering if i can do it for many objects, for example i have a button and select object (One by one, NOT all, at the same time) and add them to (so to speak) copy List... So i can choose, which one to paste afterwards...

Thanks :)

Comment
Add comment · Show 1
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 KokodokoGames · Sep 16, 2016 at 01:07 PM 0
Share

I was wondering the same thing. I copied a gameobject that has animation clips associated with it. To my surprise the movieclips still work even with the copied gameobject!

1 Reply

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

Answer by Bunny83 · Dec 07, 2015 at 01:51 AM

Well, we can't tell how the editor actually performs a copy and paste action, however it most likely (99%) uses Unity's serialization system to serialize the selected objects when you copy them and deserialize them when pasting. At least the ISerializationCallbackReceiver callbacks are called whenever you copy / paste objects in the hierarchy.

I'm not sure what you actually want to do. Unity's serialization system only works inside the editor. You can't save things at runtime. If you want to copy multiple objects in the editor you can simply hold the CTRL key and select as many objects as you want. When you press CTRL+C you will copy all objects you have selected.

If you want to automate things inside the Unity editor you can use the Selection class in an editor script to select any object inside the editor. With EditorApplication.ExecuteMenuItem you can trigger "Edit/Copy" and "Edit/Paste".

If you want such functionality at runtime you have to do everything yourself. At runtime Unity's serialization system is not available.

As i said in the beginning it's not clear what and where / when you want copy "something". If my answer isn't enough you should edit your question and be more clear about what you want to do.

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 instruct9r · Dec 07, 2015 at 11:47 AM 0
Share

Hi @Bunny83 thanks for the answer :)

1; I was just curious, there's nothing specific, that i want to do.

If unity uses the Serialization system, then i guess it won't be possible to copy gameObject or Component at runtime and paste it after you exit runtime, but that works, even if some changes are made, while in Runtime. So i guess it doesn't use the Serialization, since you say, that, it does Not work in Runtime.

I was just thinking of some way to keep changes in the components, done in Runtime...

avatar image Bunny83 instruct9r · Dec 08, 2015 at 04:33 AM 1
Share

No, you get that wrong. By runtime i talk about the Unity engine itself. The copy and paste feature as well as the AssetDatabase and serialization system are part of the Unity editor application. If you run your game inside the editor you still have all editor features available. Once you create a build you only have the Unity engine available.

Why would you want to keep changes done at runtime? The runtime is when you're executing your game. It seems that you mix game functionality with editor functionality. You can write editor scripts to perform any special actions at edit time. What you can do is creating prefabs of objects at runtime so they are saved, even when you stop playmode. However You might loose certain references to objects which aren't saved as asset. This is not the intended use of those mechanics. The playmode exists to test your game, not to edit something.

With editor scripts you have way more flexibility compared to what you can do at runtime. With editor scripts you can actually create "real" prefab instances via code which isn't possible in game code at all.

Since you don't have a concrete usecase we can't help you any further...

avatar image instruct9r Bunny83 · Dec 08, 2015 at 10:35 AM 0
Share

There are allways some cases, where you need to adjust something, while seeing it (Speed of movement \ rotation, etc)... And by now, you have to remember the value of component, that is adjusted in Playmode and then re-add it, when exit playmode... This is why i was asking and wondering if i can keep runtime changes. I have allready tried with Prefabs, but (as you mentioned) the references to other scene objects is lost, so this doesn't work at all...

thanks for the explanation, on the Serialized Objects...

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

38 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

Related Questions

Find a Component/GameObject Using an Interface Reference 2 Answers

Disabling A Script on a GameObject From a Different Script 2 Answers

Distribute terrain in zones 3 Answers

Add component to a gameobject created through code. 1 Answer

Is it OK to use public accessers for unique gameobjects (C#) ? 3 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