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
0
Question by wknight92 · May 23, 2012 at 06:20 PM · arrayarrayselementsequalrandomizing

Setting arrays equal to each other issue

Hi,

when I want to make tempArr = masterArray, it works. However, when I change the contents of tempArr, the masterArray follows suit.

In my program I want the master array to be the unchanging list of data for the temp array to refer too. In my program I have this:

 for (var i : int = 0; i<masterArray.length; i++){
     tempInt = Mathf.Round(Random.Range(0, tempPosArray.length)); //random index
     tempArrayP[i] = tempPosArray[tempInt]; 
     tempPosArray.RemoveAt(tempInt);
 }
 tempPosArray = masterArray; 

This function makes a new array(tempArrayP) which is the same thing as tempPosArray with a randomized order. In order to make this happen, I pick a random element out of the array, I plug it into the new temp array and remove it from the old one So that there are no duplicates.

The problem: when I remove elements from tempPosArray, those same elements are moved from masterArray as well...and WHY? masterArray is on the left side of the equal sign...I just don't get it. is there a way to make masterArray concrete, unchanging. masterArray is set in the Start (); function.

Comment
Add comment · Show 5
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 wknight92 · May 23, 2012 at 06:32 PM 0
Share

bumppppppp

avatar image hijinxbassist · May 23, 2012 at 06:37 PM 0
Share

You are sure there is no place in you code that says something like

 masterArray=oneOfTheOtherArrays;

Since that will modify the masterArray ins$$anonymous$$d of the other array. You said "masterArray is on the left side of the equal sign" which will modify the masterArray and not the other, $$anonymous$$d posting the other lines where masterArray is compared to another array.

avatar image wknight92 · May 23, 2012 at 06:44 PM 0
Share

oops, I meant 'right side' as shown above where 'tempPosArray = masterArray;'

this function is called through a Send$$anonymous$$essage on startup: function GetOriginalPosition (theirRelativePosition : Vector3){ masterArray[iP] = theirRelativePosition; iP++; }

This function is called right before the for loop in my OP. function RefreshValues () { tempPosArray = masterPositionArray; sendIndex = 0;
}

avatar image wknight92 · May 23, 2012 at 06:46 PM 0
Share

the first function is where masterArray is set, the second function is where it is used(plugged into temp array)

oh by the way, masterArray is the same thing as masterPositionArray

avatar image robertbu · Mar 15, 2013 at 01:50 AM 0
Share

If you are doing the above just to randomize the array, a simpler method is to shuffle the array in place. Just repeatedly swap randomly selected elements a bunch of times.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by GamesRUs · May 23, 2012 at 06:59 PM

Using the assignment operator, you are actually just referencing the original Array. To make a copy, instantiate the new array with the same size as the source array, then iterate through the original array and assign the individual values to the same index within the new array.

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 wknight92 · May 23, 2012 at 07:17 PM 0
Share

this makes sense thank you!

avatar image
1

Answer by hathol · May 23, 2012 at 06:53 PM

You have to understand that arrays are reference types. That means, that they don't actually store values, they only store references to those values. So when you have a line of code like

 tempPosArray = masterArray; // called before  your function as well I suppose

what you actually do is copying a reference to a memory location, meaning that any changes to the memory at that location (including removing elements) will be reflected in both arrays. That was the "why" part. Now to change this, just use

 tempPosArray = Array.copy(masterArray, tempPosArray, masterArray.Length);
Comment
Add comment · Show 5 · 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 wknight92 · May 23, 2012 at 07:14 PM 0
Share

what is Array.copy();? I don't see it anywhere in the Unity API.

avatar image hathol · May 23, 2012 at 08:11 PM 0
Share

Array.Copy is a c# function that copies on array into another. http://msdn.microsoft.com/en-us/library/system.array.copy(v=vs.71).aspx

sry, I didn't realize you were using JS. Use the method @GamesRUs proposed ins$$anonymous$$d then :)

avatar image GamesRUs · May 23, 2012 at 08:43 PM 0
Share

Yes, I started to say to use Array.Copy until I noticed it was JS. :)

avatar image MountDoomTeam · Mar 15, 2013 at 12:14 AM 0
Share

it almost works, for me it said Assets/$$anonymous$$archingCubes3/@Scripts/tunnels.js(36,51): BCE0022: Cannot convert 'void' to 'float[]'.

 distances_sort = System.Array.Copy(distances, distances_sort, nodes.Length);
avatar image GamesRUs · Mar 15, 2013 at 01:13 PM 0
Share

You do not need the 'distanaces_sort = ' portion as the Array.Copy method does not return a value. What it does is takes the array in parameter one (distances) and copies it into the array in parameter two (distances_sort) starting with the first element and stopping with the element index specified in parameter three (nodes.Length in your case). Are you sure that you don't want to use distances.Length ins$$anonymous$$d of nodes.Length though?

In short - the error you received is telling you that Array.Copy returns void and you are trying to assign void to a variable of type float[] (an array of floats which is the distances_sort array).

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Using class arrays for multiple Variables per element also Naming Elements 2 Answers

Problems with Arrays 2 Answers

Array Within Index 1 Answer

my QandA array is not working when you choose 3 wrong answers 1 Answer

Array empties values on RunTime? 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