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 Tialkannos · Sep 27, 2011 at 05:03 PM · array

Array copy

If I assign an array to another, both always keeps the same values. So what is the best way to copy the values from one array to another and still keep the arrays independent? (I know I can assign each element separately, but would this be the most efficient way?)

 var first : int[] = new int[1];
 var second : int[] = new int[1];
 
 function Start ()
 {
     first[0] = 1;
     second[0] = 2;
     Debug.Log("first:"+first[0]+" second:"+second[0]);
     //first:1 second:2
     first = second;
     Debug.Log("first:"+first[0]+" second:"+second[0]);
     //first:2 second:2
     first[0] = 1;
     Debug.Log("first:"+first[0]+" second:"+second[0]);
     //first:1 second:1
     
     //wish I could change the value of the first without affecting the second
 }
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
4
Best Answer

Answer by Tialkannos · Sep 28, 2011 at 03:13 PM

instead of

first = second;

the best way I could find is

System.Array.Copy(second,first,1);

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
avatar image
-3

Answer by yeoldesnake 1 · Sep 27, 2011 at 05:22 PM

You can simply do

 first=second;

That way it copies all of the properties of the second array into the first one , including length.

Another redundant way would be a for loop:

 var forPointer:int=0;
 
 for(;forPointer<first.length;forPointer++){
 first[forPointer=second[forPointer];
 }

This would return an error if the first had a bigger length than the second one.

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 Tialkannos · Sep 27, 2011 at 06:23 PM 0
Share

$$anonymous$$y problem is that when I put first = second; the two arrays always keeps the same values, so when change one, the other also changes

with the for loop I can do what I want, but my question is if it is the best way, because my arrays are quite large

but thanks for your answer anyway

avatar image yeoldesnake 1 · Sep 27, 2011 at 06:25 PM 0
Share

There is no reason to set one equal to the other on each frame or whenever you change that one. I cant see the problem really.

avatar image Tialkannos · Sep 27, 2011 at 07:17 PM 0
Share

In my code I put this line first[0] = 1; but this change the value in the second array too, I think it is because the arrays now have the same reference (I tried to explain with the comments)

I just want to have a copy of an array, so I can compare if one value have changed. But in this way, both always have the same values, then it is useless to compare.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Find children and set them all to be kinematic rigidbodies 1 Answer

Variable values not available or they lost their value 1 Answer

Unity Engine issue... 1 Answer

question about array or list of boolean 1 Answer

Argument is out of range in array 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