x


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.

more ▼

asked Apr 11 '11 at 10:37 PM

Uriel_96 gravatar image

Uriel_96
930 61 70 81

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Apr 11 '11 at 11:35 PM

Eric5h5 gravatar image

Eric5h5
80.1k 41 132 519

perfect this really works, thanks again eric

Apr 12 '11 at 12:24 AM Uriel_96
(comments are locked)
10|3000 characters needed characters left
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.

more ▼

answered Apr 11 '11 at 10:43 PM

kennypu gravatar image

kennypu
677 12 12 24

oh, I can believe it, the solution was just to easy, thanks :)

Apr 11 '11 at 10:49 PM Uriel_96

Well, except that doesn't actually work. You can do that with the JS Array class, but nothing else.

Apr 11 '11 at 11:34 PM Eric5h5

oh, maybe that's why I was getting some complications

Apr 12 '11 at 12:14 AM Uriel_96

looks like you're correct, I was using the Array class. sorry about that. Use Eric5h5's code.

Apr 12 '11 at 12:39 AM kennypu
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5056
x3443
x1355
x819
x55

asked: Apr 11 '11 at 10:37 PM

Seen: 2301 times

Last Updated: Apr 11 '11 at 10:37 PM