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 e-bonneville · Jul 29, 2010 at 09:23 PM · arrayfor-loop

Where should I use a traditional forloop as opposed to 'for (var i in monsters)'?

I'm iterating through an array with a for loop. Here's the code:

for (var i in monsters) {
    if(i == monsterName) {      
        newEnemyName = i;           
        Debug.Log (i + 1);
    }
}

Now, here's the problem: I want to print the variable in the array after the current one. If I do i + 1 like in the code, I just get the output of i (let's say troll) with a one on the end, like this: Troll1. How do I print the variable that comes after i?

Thanks - Elliot "The Question Asker" Bonneville

P.S. I've been struggling with a particularly knotty problem for about two days now, so I've been asking a lot of questions about arrays and forloops. Please forgive me. :)

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
3
Best Answer

Answer by Mike 3 · Jul 29, 2010 at 09:28 PM

I think you just want a more traditional for loop:

for(var i = 0; i < monsters.Length; ++i)
{
    //do stuff with i (the index) and monsters[i] (the string relating to the index)
}
Comment
Add comment · Show 5 · 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 e-bonneville · Jul 29, 2010 at 10:44 PM 0
Share

Thanks, that did the trick.

avatar image Mike 3 · Jul 29, 2010 at 11:14 PM 0
Share

note - if you do monsters[i+1] now it should work, but be careful about the array bounds with it

avatar image e-bonneville · Jul 29, 2010 at 11:32 PM 0
Share

Oh, right. Glad you mentioned that.

avatar image e-bonneville · Jul 29, 2010 at 11:34 PM 0
Share

By the way, thanks to you, I've solved my problem, and my project is nearing completion.

avatar image Peter G · Jul 30, 2010 at 01:41 AM 2
Share

Just thought I'd throw this out there also. foreach loops will allocate memory if you are looping through a reference type while using a for loop then getting the arrayIndex[i] doesn't. Saves some memory if you are on iPhone.

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

rotating vertices with for loop in 0 Answers

Using for loop to gather all waypoint objects near player 2 Answers

assigning arrays with for in 1 Answer

IndexOutOfRange error on iPhone, but not on PC/Player 0 Answers

Comparing each random element in an array with each other element 3 Answers


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