x


Dynamic variable names

This might be answered but honestly couldn't find an answer.

How do I create dynamic variable names inside a loop for example? Or is this even possible?

for(i = 0; i < 10; i++) { var number+i = i; }

more ▼

asked Jan 10 '10 at 11:05 PM

Wizeon gravatar image

Wizeon
52 4 5 11

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

2 answers: sort voted first

Not sure to fully understand your question, but from the code sample you provide, it seems that you're looking for arrays.

var number:int[] = new int[10];

for(i = 0; i < 10; i++)
    number[i] = i;

See here for a documentation on arrays : http://unity3d.com/support/documentation/ScriptReference/Array.html

more ▼

answered Jan 11 '10 at 12:49 AM

Mortim gravatar image

Mortim
709 3 7 18

Hah I forgot about arrays. Thanks for reminding me :) I blame the clock, I live in GMT+2 so it's 5am here.

Jan 11 '10 at 03:17 AM Wizeon
(comments are locked)
10|3000 characters needed characters left

if you really want dynamic names you can use a dictionary and then add keys with dynamic names with their values. use arrays because they are the fastest solution. if you need to access array elements much and need to search much in the array just use dictionaries.

more ▼

answered Jan 11 '10 at 06:00 AM

Ashkan_gc gravatar image

Ashkan_gc
9.3k 33 56 120

(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:

x5276
x852

asked: Jan 10 '10 at 11:05 PM

Seen: 2186 times

Last Updated: Jan 10 '10 at 11:05 PM