- Home /
Question by
iloveaphextwin · Dec 06, 2011 at 10:18 AM ·
arraynamenamingunique
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
Comment
Answer by Statement · Dec 06, 2011 at 10:24 AM
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;
Thanks for that. I guess it might be better to write all the code using the array numbers.
Yes, it's to prefer over finding by name, converting to string, generating a lot of garbage memory etc :)