- Home /
array.length read only? workaround?
hello guys. Is there anyway of changing array.length through script?
I am basically trying to make a backup var for a array. The length of the array may change with deferent situations.
also I need something that works in javascript.
thanks.
Answer by Eric5h5 · Nov 24, 2010 at 03:53 AM
You change the length of the array by creating a new one and copying the values over. (There's a Resize function for built-in arrays, but it only works directly in C#, and even then what it actually does is create a new array anyway.) If you're talking about the Javascript-specific Array class, you can add or remove elements.
Answer by by0log1c · Apr 21, 2011 at 06:09 PM
As Eric mentionned. Javascript Array will initialize new element the first time you reference them, so no need to worry about size/length.
As for built-in, I usually use something like this:
var myStrings:String[] = new String[9];
function AddString(str:String){
var buffer:String[] = myStrings;
myStrings = new String[buffer.length+1];
for(i=0;i<buffer.length;i++){ myStrings[i] = buffer[i]; }
myStrings[myStrings.length-1] = str;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613084305im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Set variable/Array to a length by scripting 2 Answers
Instantiate into array : Out of range? 1 Answer
Arrays in shaders 0 Answers
random spawn locations.... 2 Answers