Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 pimentelu_ · Jun 16, 2020 at 04:14 PM · loopforeach

foreach loop stops at first item

hi! i have this is script and my foreach is not working properly, when i try to debug something inside it, it works perfectly, but if i put a if or for inside it, it only goes through it once

 public List<GameObject> allExercises = new List<GameObject>();
 public string[] exercisesSplitString;
 
 public void LoadData()
     {
           foreach(GameObject item in allExercises)
                 {
                     Debug.Log("hoo");
                     if(item.name == exercisesSplitString[0])
                     {
                         Debug.Log("heeey");
                     }
                 }

Some things to consider: - the first item name in allExercises IS the same as exercisesSplitString[0] - it doesn't debugs the 'heeey', only the 'hoo' and only once - if i erase the 'if', it debugs the 'hoo' correctly (which is 5 times) - obviously i'm not only trying to debug things, but i'm trying with debug to see if i can fix it before doing what i want to

thank you so much!

Comment
Add comment · Show 5
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 pimentelu_ · Jun 16, 2020 at 04:27 PM 0
Share

i tried something different to see if the foreach works

 foreach(GameObject item in allExercises)
                 {
                     Debug.Log("heey");
                     if(2 == 2)
                     {
                         Debug.Log("hoo");
                     }
                 }

i know it seems weird to compare 2 and 2, but it actually worked. then i tried this:

 foreach(GameObject item in allExercises)
                 {
                     Debug.Log("heey");
                     if(2 == 2)
                     {
                         Debug.Log("hoo");
                         GameObject.Find("1").GetComponent<Toggle>().isOn = true;
                     }
                 }

and the foreach only worked once and didn't turn the toggle i wanted on.

avatar image ShadyProductions · Jun 16, 2020 at 04:38 PM 0
Share

Why don't you do

 Debug.Log("Item name: " + item.name + " | exercise name: " + exercisesSplitString[0]); 

and compare the 2 to find difference :)

avatar image pimentelu_ ShadyProductions · Jun 16, 2020 at 06:16 PM 0
Share

just tried that, this time it didn't sent me the 'hoo' nor the things i asked in this new debug, i feel like there's something wrong with the foreach, could you check my comment bellow? i tried other things and it didn't work either

avatar image ShadyProductions pimentelu_ · Jun 16, 2020 at 08:01 PM 0
Share

It's unclear what you are trying to achieve.

I see this:

 public List allExercises = new List();

But if you initialize it in the inspector you don't need the = new List<GameObject>(); part.

avatar image Bajtix · Jun 16, 2020 at 09:14 PM 2
Share

Is the public string[] exercisesSplitString;initialized anywhere? Are you sure you are not getting any errors in the console?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by bentoBAUX · Feb 14 at 02:35 PM

I have faced a similar problem. My solution was to simply use a coroutine instead. For example, in your case you could do:

 IEnumerator LoadData(){
          foreach(GameObject item in allExercises){
                      Debug.Log("hoo");
                      if(item.name == exercisesSplitString[0]){
                          Debug.Log("heeey");
                          yield return null;
                      }else{
                          //whatever you wanna do
                          yield return null;
                      }
           }
 }


And you could simply call this coroutine via:

 StartCoroutine(LoadData());

in whichever function you wish.

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 rh_galaxy · Feb 14 at 02:50 PM 1
Share

I don't think this is a solution to the problem, it only spreads the loop to do one item per frame instead of doing it all in one go.

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

132 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 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 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 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 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 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 create a variable for each GameObject that I added to an array by using a loop? 2 Answers

Array Element in Foreach loop 1 Answer

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

Using ForEach Loop to change variables on prefabs 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