Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by headkit · Apr 27, 2010 at 03:20 PM · variablearraysnamecountiterate

iteration through objects - how can I step through numerically named variables? (or nested / jagged 2D arrays?)

hi folks!

how could you iterate through a set of objects with almost the same name, like

var theTextpair_1 = ["Foo", "Bar"]; var theTextpair_2 = ["Foo", "Bar"]; var theTextpair_3 = ["Foo", "Bar"];

for(i=1; i<=3; i++) { for(j=0; j<=1; j++) { print("theTextpair_"+i[j]); } }

thanx!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by duck · Apr 27, 2010 at 03:39 PM

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:

var textPair1 = ["Foo", "Bar"]; var textPair2 = ["Foo", "Bar"]; var textPair3 = ["Foo", "Bar"];

// create an array of arrays (a.k.a. a jagged array) var textPairs = [ textPair1, textPair2, textPair3 ];

for(int pairNum=0; pairNum < textpairs.Length; pairNum++) { var textPair = textPairs[pairNum]; for(int itemNum=0; itemNum < textPair.Length; itemNum++) { print("the text pair "+pairNum+","+itemNum+" = "+ textPair[itemNum]); } }

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.

  • Array indices in Unity are zero-based. So the first item is item 0, the second item is item 1. Hence the changes to the "for" loops above. They start at zero, and then continue while the index is less than the array length. The last item in an array containing 10 items is thatArray[9].

  • If you're actually working with sets of pairs, you don't really need to iterate over the pairs, you could just access them using

    print ("the text pair is: " + textPair[0] + ", "+ textPair[1]);
    
  • There are other types of collections which are even better suited to storing pairs of items, if those pairs represent key-value pairs. The most commonly used are the HashTable and the Generic Dictionary (the latter is c# only).

  • Comment
    Add comment · Show 3 · Share
    10 |3000 characters needed characters left characters exceeded
    ▼
    • Viewable by all users
    • Viewable by moderators
    • Viewable by moderators and the original poster
    • Advanced visibility
    Viewable by all users
    avatar image headkit · Apr 28, 2010 at 07:34 AM 0
    Share

    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]

    avatar image duck ♦♦ · Apr 28, 2010 at 11:34 AM 0
    Share

    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).

    avatar image duck ♦♦ · Apr 28, 2010 at 11:34 AM 0
    Share

    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 program$$anonymous$$g 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).

    Your answer

    Hint: You can notify a user about this post by typing @username

    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

    Answers Answers and Comments

    No one has followed this question yet.

    Related Questions

    Naming array elements in editor 3 Answers

    Iterating through shuffled/randomized array issue 0 Answers

    How to count and name total number of game object are there in my scene? 1 Answer

    Is it bad to use long variable names? 1 Answer

    Call static variable dynamically 1 Answer


    Enterprise
    Social Q&A

    Social
    Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

    Footer

    • Purchase
      • Products
      • Subscription
      • Asset Store
      • Unity Gear
      • Resellers
    • Education
      • Students
      • Educators
      • Certification
      • Learn
      • Center of Excellence
    • Download
      • Unity
      • Beta Program
    • Unity Labs
      • Labs
      • Publications
    • Resources
      • Learn platform
      • Community
      • Documentation
      • Unity QA
      • FAQ
      • Services Status
      • Connect
    • About Unity
      • About Us
      • Blog
      • Events
      • Careers
      • Contact
      • Press
      • Partners
      • Affiliates
      • Security
    Copyright © 2020 Unity Technologies
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell My Personal Information
    • Cookies Settings
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    • Anonymous
    • Sign in
    • Create
    • Ask a question
    • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Explore
    • Topics
    • Questions
    • Users
    • Badges