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 DaceProducer_Official · Feb 03, 2015 at 04:48 PM · c#gameobjectlistforeach

C# List foreach problem

Hello everyone. I'm working on a game for Android where the player needs to touch some objects spawning on random locations before they auto-disappear. The problem I'm having is with deactivating the objects:

 IEnumerator DestroyCircles ()
 {
     foreach (GameObject o in circles)    //Where circles is a List<GameObject>
     {
         yield return new WaitForSeconds (destroyTime);
         circles[o].SetActive (false);
     }
 
     yield break;
 }

I get the following two errors:

1) The best overloaded method match for System.Collections.Generic.List<UnityEngine.GameObject>.this[int]' has some invalid arguments; 2) Argument #1' cannot convert UnityEngine.GameObject' expression to type int'.

For the first error, it doesn't work if I convert "o" to an int (obviously).

How should I go about this? Any help is greatly appreciated.

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

Answer by karljj1 · Feb 03, 2015 at 04:50 PM

Your trying to access circles with an array index but using a GameObject instead of an int as an index.

Looks like you have confused a foreach and a for loop. The variable o will be the next item in the list so just use o.

circles[o].SetActive (false);

should be

o.SetActive (false);

EDIT:

Ok i think this is what you want:

 IEnumerator DestroyCircles ()
  {
      yield return new WaitForSeconds (destroyTime); // Put a pause in before we disable the objects
      foreach (GameObject o in circles)  // Now disable them all in one go.
      {      
          o.SetActive (false);
      }   
  }

Comment
Add comment · Show 7 · 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 Bonfire-Boy · Feb 03, 2015 at 05:07 PM 2
Share

I think he was going round in circles.

I'll get my coat

avatar image DaceProducer_Official · Feb 03, 2015 at 08:55 PM 0
Share

lol Thanks for the answer, but I get this error now:

InvalidOperationException: Collection was modified; enumeration operation may not execute. System.Collections.Generic.List`1+Enumerator[UnityEngine.GameObject].VerifyState () (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Collections.Generic/List.cs:778) System.Collections.Generic.List`1+Enumerator[UnityEngine.GameObject].$$anonymous$$oveNext () (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Collections.Generic/List.cs:784) CircleClassic+c__Iterator0.$$anonymous$$oveNext () (at Assets/Scripts/CircleClassic.cs:54)

avatar image karljj1 · Feb 03, 2015 at 09:03 PM 0
Share

Did you make a change to the list whilst the coroutine was running? Use a for loop, it wont cause these errors.

 for( int i = 0; i < circles.Count; ++i )
 {
     circles[i].SetActive( false );
     yield return new WaitForSeconds (destroyTime);
 }

Or you could put them all into a second list an iterate through that.

avatar image Landern · Feb 03, 2015 at 09:05 PM 0
Share

It's true you can't modify the List/Collection/Whatever during a foreach, you should however be able to set properties as long as you're not removing or adding to the collection/list that is being iterated over. SetActive shoould not add or remove elements from the collection. This begs the question of whether you're modifying/destroying anything after the yield statement in the foreach loop? If so, that is the source of your issues.

avatar image Gizmoi · Feb 03, 2015 at 09:27 PM 1
Share

If you don't want them to disappear one after the other, remove the yield return new WaitForSeconds(destroyTime) then they will all be destroyed at once.

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How would I find the name of all game objects in a c# List 1 Answer

For loop does not loop C# 2 Answers

cant get characters to load 1 Answer

How to put gameObjects to the list? 4 Answers

C# Trouble testing whether a list of game objects contains a specific game object 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