- Home /
Problem when trying to save variable in another variable.
Hi.
I don't know if its a bug, but when I try to save a variable to another variable in a loop, and change the first variable after, the second one gets change too.
Code:
//example
var variable : Array;
for (i = 0; i < 5; i++) {
var variable2 = variable;
variable.Clear ();
//variable2 becomes empty too
}
"Variable" needs to get cleared because it will contain stuff to use in the next loop, but the stuff will be added this loop.
I'm sure I missed something... Any ideas?
Thanks
Answer by fafase · Sep 03, 2013 at 04:06 PM
Arrays are passed by reference so you are not copying your array you are passing the address of it to the other. Now you have two variables pointing at the same spot.
You need to copy the whole array first and then clear it. Then variable2 will have the values.
Your answer
Follow this Question
Related Questions
Create new variable name based on array index 3 Answers
variables based on array.length not updating in JS 1 Answer
Array with boolean variables? 3 Answers
Global Variables Refuse to Cooperate 1 Answer