|
hi folks! how could you iterate through a set of objects with almost the same name, like
thanx!
(comments are locked)
|
|
It looks as though you're looking for something which resembles a 2D array. There are two methods of implementing a 2D (or multi-dimensional) array in Unity. There are "real" multi-dimensional arrays, and there are "Jagged" arrays. The difference is this: With a "real" 2D array, your array has a fixed "width" and "height" (although they are not called width & height). You can refer to a location in your 2d array like this: myArray[x,y]. In contrast, "Jagged" arrays aren't real 2D arrays, because they are created by using nested one-dimensional arrays. In this respect, what you essential have is a one-dimensional outer array which might represent your 'rows', and each item contained in this outer array is actually an inner array which represents the cells in that row. To refer to a location in a jagged array, you would typically use something like this: myArray[y][x]. Converting your script above to use a jagged array, would look something like this:
Due to a quirk of Unity's Javascript, you can't define a true 2D array in a JS script (only in Unity's C#), however you can work with them. Read more about that here. However, Your question throws up a few interesting points.
thanx for the answer, but i didn't search for a solution where i need another array. i was searching for something similiar to the AS3-construct like root["textPair_"+i][j]
Apr 28 '10 at 07:34 AM
headkit
Well, this is generally how you should be doing it - either with 2d arrays, or with a HashTable or Generic Dictionary, or some other appropriate container which is designed to be used in this way (as mentioned at the end of my answer).
Apr 28 '10 at 11:34 AM
duck ♦♦
It is possible to use strings to directly reference variable names, however you'd have to use .net's "reflection" features to do it, but to be frank (and please don't take this personally), piecing together variable names in the way that you're trying to do is generally seen as bad programming practice for various reasons, and is indicative of a lack of understanding of the more appropriate tools within the language. This goes for AS3 as well as C# and Unity's Javascript (AS3 also has a "Dictionary" class, very similar to .net's).
Apr 28 '10 at 11:34 AM
duck ♦♦
(comments are locked)
|
