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 acehwk · Jun 22, 2013 at 07:23 PM · c#listforeachgame object

C# Trouble testing whether a list of game objects contains a specific game object

Hello. First time help seeker, and I greatly appreciate any help you guys can give.

I want to build a list of game objects based on a certain criteria. To keep this example simple, let's say that this list will be game objects that are within sight of the player at a specific frame X. Then I want to later test a selected game object to see if it was spotted by the player during frame X.

Here's a simplified version of the code:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class PlayerControls : MonoBehaviour {
 
      public List <GameObject> spotted = new List<GameObject>();
      public GameObject enemyObject; // starts as null, used to hold a specific
                                     // object for testing, then set to null again
 
      void ScanArea()
      {
           RaycastHit hit;
 
           if (Physics.Linecast(player.position, target1.position, out hit))
           {                        
                if (currentHit.collider.gameObject.tag == "Enemy")
                {
                     spotted.Add(currentHit.collider.gameObject);
                }
           }
           // multiple targets will be linecasted in this way
      }
 
      void TestGameObject()
      {
           enemyObject = // some object that is going to be tested
 
           foreach (GameObject obj in spotted)
           {
                if (enemyObject == obj)
                {
                     DoSomething();
                }
           }
      }
 }
 

What is happening is that the test works when I test the object that happened to be the first object added to the list "spotted" but if I test any of the other objects that I know are in that list, they do not work.

I have used a workaround where I alter the color of spotted objects then test for color when I do my test, but that seems unnecessary and unideal. I'm sure I'm missing something obvious but I can't seem to figure out what :X

EDIT: looks like this is the error that might be causing me troubles: "InvalidOperationException: Collection was modified; enumeration operation may not execute." I'm not really sure why I'm getting this error though.

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

Answer by acehwk · Jun 22, 2013 at 07:41 PM

Well I hate to be that guy but after hours and hours of failing to fix the problem. I think I have found a solution within 10 minutes of asking for help.

I added "break" after DoSomething and made DoSomething a Coroutine.

DoSomething was actually clearing my list "spotted" while the list was still being used in foreach which was causing the error.

Comment
Add comment · 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

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

15 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

Related Questions

For loop does not loop C# 2 Answers

"enumeration operation may not execute" Problem with C# list 1 Answer

foreach loop in OnCollisionStay 1 Answer

C# List foreach problem 1 Answer

null reference exception when I already checked if this was null 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