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 wolga2 · Oct 01, 2013 at 06:21 AM · loopforeach

how to use two foreach loops into another correctly

i have two foreach loops into another. just for correct understanding, if would have in both foreach loop two objects that are looped through, so both of the first two would check with the both of the loop inside, so there would be four check? but if i use a code like this, the first two objects will check only with the last of the two inside gameobjects. cn someone tell me what i'm thingking wrong and if there is a correct way to use two foreach loops? thank you!

 foreach (GameObject go in gos)
 {
    foreach (GameObject otherGo in otherGos)
    {
       // check something
    }
 }
Comment
Add comment · Show 1
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 FrankStarsKo · Oct 01, 2013 at 06:30 AM 0
Share

does this help? http://docs.unity3d.com/Documentation/$$anonymous$$anual/InstantiatingPrefabs.html

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Hoeloe · Oct 01, 2013 at 09:52 AM

Fattie is right, you should make sure to mark correct answers as such, so that the question can be closed.

Aside from that, your issue is with what are known as "nested loops". Loops work like this: All the code inside the {} enclosing the loop will be executed for each element in the loop. This includes other loops. If, for example, your arrays gos and otherGos each have 2 elements, and let's assume each holds 2 integers, so gos = {1, 2} and otherGos = {3, 4}, and you run this code:

 foreach (int go in gos)
 {
     foreach (int otherGo in otherGos)
     {
         Debug.Log(go + " : " + otherGo);
     }
 }

(This is virtually identical to the code you've written up there. The only difference is the types have been swapped, and a log statement added in to demonstrate), you will get the following output:

 1 : 3
 1 : 4
 2 : 3
 2 : 4

Why is this? Well, the code runs through each member of gos in turn, so it runs all the code for the first value (1), then repeats it all for the second value (2). So, in the first iteration of the loop, it runs:

 foreach (int otherGo in otherGos)
     {
         Debug.Log(go + " : " + otherGo);
     }

with go set to 1. When it hits the other loop, however, it runs through all the elements of otherGos in turn, too, but the key point is that it doesn't exit the previous loop, so while it's doing this, the first loop is still in its first iteration (that is, with go set to 1). When this inner loop has finished (this first iteration of the outer loop accounts for the first 2 print statements), only then does it return to the outer loop and look at the next element, setting go to 2, and then repeating the inner loop with go at 2. This accounts for the next 2 print statements. You could extend this further. If you ran exactly the same code with gos = {1, 2, 3, 4} and otherGos = {5, 6, 7}, you would get:

 1 : 5
 1 : 6
 1 : 7
 2 : 5
 2 : 6
 2 : 7
 3 : 5
 3 : 6
 3 : 7
 4 : 5
 4 : 6
 4 : 7
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 wolga2 · Oct 01, 2013 at 02:53 PM 0
Share

I'm sorry. I went through all my questions and thicked them! Thank you very much for your detailed answer! That is exactly how I imagined the two foreach loops to work and also how I used them. But the problem is, it will only return me the value of one bool (if I log it) this way. But it actually should return me the four different combiantions of the bools, right? For example something like true : true, true : false, false : true, false : false or so. Why doesn't this work?

 foreach (int go in gos)
 {
     if (someCondition)
     {
         someBool = true;
     }
     else
     {
         someBool = false;
     }
     foreach (int otherGo in otherGos)
     {
         if (someOtherCondition)
         {
             someOtherBool = true;
         }
         else
         {
             someOtherBool = false;
         }
     }
 }
avatar image Hoeloe · Oct 01, 2013 at 09:39 PM 0
Share

Please, when asking a question, don't EVER ask "why doesn't it work?". That is the vaguest, most unhelpful thing you could ask. Tell us what it is doing, in comparison to what you expect it to do. When you say it doesn't work, HOW doesn't it work? Does it give you errors? Does it always give you "true"? Does it not give you anything?

avatar image wolga2 · Oct 01, 2013 at 11:49 PM 0
Share

yeah right, sorry. i just thought it would be a basic mistake a beginner makes anyway and it is for you actually obvious why this with the bools can not work. that's why i was asking the 'why doesn't it work' question. it doesn't give any errors. the thing is that if i log it just one bool is shown. if i press play, the scripted reaction to the bool works for the two 'go' in 'gos' and for the last 'otherGo' in 'otherGos', but none of the 'go' will check with the first 'otherGo' in the array. if i log as you said (go + " : " + otherGo); in the secound foreach loop, it will show all four possibilities. if i log (someBool + " : " + someOtherBool) it, as i said, just gives me back one single bool combination. i tried to explain myself that this is because bools can just store one information. if i check these foreach loops with floats ins$$anonymous$$d of bools, it will log the four combinations again. so i solved the problem with storing the needed information by changing float values and then ask for them ins$$anonymous$$d of checking if one of the bools is true or false. thanks for the answers!

avatar image Fattie · Oct 02, 2013 at 06:53 AM 0
Share

Hi there @wolga, can you please TIC$$anonymous$$ an answer, to close out the question. Only you can do it, as the questioner. Thanks. It's the round tick symbol.

avatar image Hoeloe · Oct 02, 2013 at 10:54 AM 0
Share

I'd like to see the code you tried for your attempt with booleans, because it should have worked perfectly. Also, as a general rule, avoid floats if you possibly can, they are prone to horrible inaccuracy.

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

17 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

Related Questions

Using ForEach Loop to change variables on prefabs 1 Answer

Raycasting and foreach loops 1 Answer

"not all code paths return a value" i really try it and i really don't have idea 3 Answers

How to have a loop run itself again if returned value is a known exception, with a new return value. 1 Answer

Not able to make a foreach loop 2 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