Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Hachley · May 16, 2014 at 06:43 PM · arrayloopfor

[Solved]Using multiple arrays in one statement?

Hello. I have two arrays(firstArray & secondArray), and what I want to do is go through all of the variables inside them through a for loop function, with the help of another array(arrayIndex), just to preserve the code size.

 var arrayIndex = new Array("firstArray","secondArray");
 var firstArray = new Array("f0","f1","f2","f3");
 var secondArray = new Array("s0","s1","s2","s3");

 for(var x = 0; x <= arrayIndex.length;x++) {
     for(var i = 0; i <= 3;i++) {
     Debug.Log((arrayIndex[x]).[i]); //★ this doesn't work the way I need
     }
 }

Can anyone point me out the way to make (★) work? And is this the right way of doing it, or should I just use a different dimension Array for this? If you could give me an example, that would be great. Thanks in advance.

Comment
Add comment · Show 3
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 jeebxoxo · May 16, 2014 at 06:56 PM 1
Share

What exactly is happening with this? I can't tell what you're trying to accomplish.

(arrayIndex[x]).[i] doesn't make a lot of sense. It's like you're trying to reference two different indexes on the same array.

If you could explain a bit why you need the 'arrayIndex' that could help. It seems like it would be easier without it.

avatar image AlucardJay · May 16, 2014 at 07:09 PM 1
Share

sounds like you need a multi-dimensional array, for example :

 var firstArray : String[] = ["f0","f1","f2","f3"];
 var secondArray : String[] = ["s0","s1","s2","s3"];
 
 var multiStringArray : String[,] = new String[ 2, 4 ];
 
 for ( var x : int = 0; x < 2; x ++ )
 {
     for ( var y : int = 0; y < 4; y ++ )
     {
         switch ( x )
         {
             case 0 : 
                 multiStringArray[ x, y ] = firstArray[y];
             break;
             
             case 1 : 
                 multiStringArray[ x, y ] = secondArray[y];
             break;
         }
     }
 }

just off the top of my head, syntax may not be correct

Edit : I really should be asleep so the above code is terrible. You could also create a list of lists (I personally never use array, only built-in arrays or List)

avatar image Hachley · May 16, 2014 at 08:54 PM 0
Share

Thanks alucardj, just what I needed. I'm sorry to those who didn't understand what I meant, but thanks for response!

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by FarbrorMartin · May 16, 2014 at 07:52 PM

I think I understand what you are trying to do. It would be easier to use a javascript object to hold your two arrays. Like this:

 var arrays = 
 {
   "first" : [ "f0", "f1", etc... ],
   "second" : [ "s0", "s1", etc... ]
 }


Then you can access your arrays like this:

 var valueFromFirst = arrays.first[2];
 var valueFromSecond = arrays.second[0];
Comment
Add comment · Show 1 · 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 Hachley · May 16, 2014 at 08:55 PM 0
Share

Ok, thanks.

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

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how do i make a loop for a boolean? 2 Answers

Adding an object to an array only if it is not in the array 2 Answers

How to correctly shorten this script using arrays and iterations 2 Answers

Can someone give me an example on looping through arrays? 0 Answers

crazy loop !!! HELP ME 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