- Home /
Declare array in other function = error
Declaring an array in Start() is fine:
function Start () {
var array = new Array ();
var val = Vector3(1, 0, 0);
array.Push(val);
print (array);
}
however adding to the array from another function doesnt do anything, it prints an empty array:
function Start () {
var array = new Array ();
adding();
print (array);
}
function adding() {
var val = Vector3(1, 0, 0);
array.Push(val);
}
Answer by Eric5h5 · Oct 03, 2012 at 05:56 PM
Variables must be declared outside functions if they are to be accessed by more than one function. Also, don't use the Array class. Use built-in arrays such as int[] for fixed-size arrays, or generic Lists otherwise.
thanks. i just wanted to code a dungeon generator in under an hour, so i thought it should be possible to use the .js array as it is abit easier. ok no worries. the other array is easy also, i just was challenged that i couldnt use the .js one !
it was just to check a concept out taking $$anonymous$$imum time, will rewite to builtin in necessary. in end managed slow array by declaring both outside of functions: and using in both fcts thanks!
var mov : Vector3;
var array = new Array();
Use List ins$$anonymous$$d of Array; it's just as easy, but has much better speed and is less prone to errors.
var list = new List.<Vector3>();