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 Rufalo · Mar 28, 2010 at 04:45 AM · arrays

Add a copy of a object to Array

Hi im new to Programming and i have this problem with Arrays. I know how to Create and add to arrays but i dont know how to add a copy of a Object into the Array

what i mean is i have this GameObject that has some stats on it then i add that Object to the Array 2 times

the problem is when i modify the object in the array all of the objects get modified the same way

is there way that when i add the GameObject to the array it creates a copy then adds it to the array so when i edit array[1] array [0] stays the same even tho it was created from the same Object

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
4
Best Answer

Answer by qJake · Mar 28, 2010 at 07:13 AM

You've run into what's known as "Referencing" in programming. An object, by default, is always passed around by reference. This means that no matter how many times you "copy" it or pass it around, it always points to the same location in memory, and it's always the same object (and only one of that object).

To create a copy of an object, you need to do what's known as cloning, though in Unity specifically, it's a bit easier.

With Unity, you can just Instantiate a new object based on the current one, which makes an identical copy of it.

// C# GameObject myClonedObject = Instantiate(myObject) as GameObject;

// JS var myClonedObject = Instantiate(myObject);

This creates a copy of the myObject object and places it inside the myClonedObject variable.

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 Rufalo · Mar 28, 2010 at 03:40 PM 0
Share

Thanks but i have little problem that method Works Perfect with GamObjects but how do i Clone Classes? like if i have a Character Class and i want to add NewChar:Character to an Array Instantiate did not work

avatar image qJake · Mar 28, 2010 at 09:45 PM 0
Share

It's not easy to clone a class via program$$anonymous$$g, and it's not widely used. There are two forms of cloning - high level and low level cloning. I don't know what your code looks like, but I assure you, there's about a 99% chance that cloning is NOT what you're looking for. You should try and instantiate multiple objects if you want multiple instances of the same type of object.

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

No one has followed this question yet.

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Why does unity expect a file when I try to put text on a mesh? 1 Answer

How can I check values of all array/List quickly 1 Answer

I'm trying to sort my Npc's into different social classes 1 Answer

Attempting to add object to dynamic array causing problems 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