- Home /
Set variable/Array to a length by scripting
In my script is getting complex and I get a point that I don't want to set the length or size of a variable/Array so I would like to know how I can set a variable length using a variable.
Examples: (this ways doesn't work, that's why in need the correct form to do it)
var example : float[mylength];
//or also
example.Length = myLength/*or myLength.Length*/;
so that way I don't have to put
var example : float[];
and then put the length or size by manually.
Answer by Eric5h5 · Apr 11, 2011 at 11:35 PM
You can do this:
var example : float[];
function Start () { example = new float[myLength]; }
I'm not clear if that's what you're trying not to do, but if so, I don't know why.
Answer by kennypu · Apr 11, 2011 at 10:43 PM
var example: float[];
example[myLength] = 0;
now the example variable is length of myLength. I believe you can only set the length like this once. If you do this at one point, and then you try to set a value at a greater length (eg. example[myLength+1]) it will error.
oh, I can believe it, the solution was just to easy, thanks :)
Well, except that doesn't actually work. You can do that with the JS Array class, but nothing else.
looks like you're correct, I was using the Array class. sorry about that. Use Eric5h5's code.