x


Naming array elements

Hi there,

I am working on a tile based game. I have the tiles laying out perfectly. The problem is how to name them and then access them again. I have named them as below.

pieces[x,z].name = "p"+x + "p" + z;

so it should be named p1p1, p1p2 etc.

How do I now access these pieces. Like p2p3.x = 50 or something.

Thanks

Will

more ▼

asked Dec 06 '11 at 10:18 AM

iloveaphextwin gravatar image

iloveaphextwin
11 3 4 4

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

1 answer: sort newest

If you have the array:

var pos = pieces[2, 3].transform.position;
pos.x = 50;
pieces[2, 3].transform.position = pos;

If you want to use GameObject.Find (slow):

var pos = GameObject.Find("p2p3").transform.position;
pos.x = 50;
pieces[2, 3].transform.position = pos;
more ▼

answered Dec 06 '11 at 10:24 AM

Statement gravatar image

Statement ♦♦
20.1k 35 70 175

Thanks for that. I guess it might be better to write all the code using the array numbers.

Dec 06 '11 at 10:27 AM iloveaphextwin

Yes, it's to prefer over finding by name, converting to string, generating a lot of garbage memory etc :)

Dec 06 '11 at 10:34 AM Statement ♦♦
(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:

x1355
x143
x17
x5
x4

asked: Dec 06 '11 at 10:18 AM

Seen: 652 times

Last Updated: Dec 06 '11 at 10:34 AM