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
1
Question by Tomer-Barkan · Oct 13, 2013 at 03:12 PM · instantiatemonobehaviour

How does Instantiate() clone an object

Hi,

I searched but couldn't find any details on how exactly Instantiate() clones an object, specifically when that object has scripts attached to it, and those script have class variables.

So for example:

 class test1 : MonoBehaviour {
     public int num;
     public List<int> multiNum;
     public MyClass myObj;
 }

now, if I instantiate an object with test1 script attached to it, will it clone the value of num variable? What about multiNum variable? And last, what about myObj which is a class that I created. Will it clone it? If yes, then how does it clone it?

Thanks

Comment
Add comment · Show 2
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 Eric5h5 · Oct 13, 2013 at 03:45 PM 0
Share

Can't you just run the code and see what happens?

avatar image Tomer-Barkan · Oct 13, 2013 at 04:03 PM 0
Share

This is just an example, I'd like to understand the underlying mechanics in general.

For instance, if the $$anonymous$$onoBehaviour had a Clone() method I'd assume it's being used and I can override it, but it doesn't, so that's out of the question. Is there some other way that $$anonymous$$onoBehaviours are cloned? I can't figure that with trial and error.

1 Reply

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

Answer by whydoidoit · Oct 13, 2013 at 04:10 PM

It will make a copy of anything the serialization system can normally save so all basic types, all classes marker Serializable, lists and arrays. It will not copy anything else (like Dictionaries etc).

All object references to components within the tree defined as descendants of the object being cloned will be fixed up to point to the copied items.

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 Tomer-Barkan · Oct 13, 2013 at 04:16 PM 0
Share

Thanks. Any recommended links to read more details about the serialization system you mentioned?

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

17 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

Related Questions

Associate objects to a prefab 1 Answer

Duplicating a prefab 1 Answer

Classes, MonoBehaviours C# 2 Answers

Start being called after Update of another GameObject? 0 Answers

How do I get a CS script in assets to be a stored monobehaviour variable 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