• 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 scradsleep · May 17, 2020 at 02:22 AM · for-loopforeachkeydictionarylock

why isn't my lock working

hey all, hope ya'll r well today ihave been looking through documentation and others answers but i cant make to seem any sence out if them or well not a case specific answer but what i have come up with is for my foreach loop is placing a lock on it so i can remove items so far i have come up with this for my dictionary removal but i am getting an error and from what i am understanding after reading alot of peoples questions and solutions that i am placing the lock in there correctly but.... Aparently not

     public void Remove()
       {
          List<SelectedInfo> selectedObjects = playerController.GetSelected();
          
          foreach (SelectedInfo unit in selectedObjects)
          {
             if (unit.isSelected)
             {
                lock (groupLookupCache)
                {
                   foreach (var item in groupLookupCache)
                   {
                      for (int i = 0; i < item.Key.Length; i++)
                      {
                         this.groupLookupCache.Remove(item.Key);
                      }
                   }
                }
             }
          }
       }

this is the error i am reciving InvalidOperationException: Collection was modified; enumeration operation may not execute. System.Collections.Generic.Dictionary`2+Enumerator[TKey,TValue].MoveNext () (at :0) RPG.Inventories.UnitSelectionGroup.Remove () (at Assets/Scripts/Inventories/UnitSelectionGroup.cs:95) RPG.Inventories.UnitSelectionGroup.Update () (at Assets/Scripts/Inventories/UnitSelectionGroup.cs:42)

would someone be able to point me in the right direction maybe some good videos of locks or removal of keys while itereating

any help would be appreciated i am making a case specific question because im having trouble understanding some of the finer details that others are talking about (my key is also a component)

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

Answer by AlirezaSH2004 · May 17, 2020 at 02:47 AM

Hello again. Foreach loop is not going to be able to continue it's work when it's modified, meaning that something has been removed, added, etc. I recommend you instantiate an identical dictionary and manipulate that and then apply the changes to the actual dictionary of your preference. So:

       foreach (SelectedInfo unit in selectedObjects)
       {
          if (unit.isSelected)
          {
                 var tc = new Dictionary<YOURDATATYPE, YOURDATATYPE>(groupLookupCache);
                 for (i = 0; i < tc.Count; i++)
                 {
                          for (j = 0; j < tc[i].Key.Length; j++)
                          {
                                   groupLookupCache.Remove(item.Key);
                          }
                 }
           }
     }

You could also go with a foreach loop.

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 scradsleep · May 17, 2020 at 04:09 AM 0
Share

. hello again you have been a tremedous help!

im getting an error on line 8 tc[i] 'i' being the error because my key is an array in the dictionary declaration? and then another one on line 10 with item because item was only used because of the foreach loop and hasn't got anyuse elsewhere

alt text

i understand what you where saying about the loop removing the object and it not being able to work when it is being modified but i thought that the lock was to stop that because that's what i thought the lock was doing was preserving a state so as to that error to not occur because it holds the for each loop like it would if we where to instantiate another dictionary.

alt text

im not sure if what i am doing with the dictionary here is bad practise or not im guessing it is because im getting a lot of problems, im so so new to coding only done a few tutorials and hacking away at someones base code and looking at unity answers and stack overflow to learn i think i had to make the dictionary component an array is because of the for loop in the other question you helped me out on i had it as a for each loop but couldn't get it to work and now i have it as an array with the for loop, that we had to ad the item and the capital k (3days if i new that capital k by the way cos i was trying item.)

i gotta go out for a bit... but ill try change it back to the foreach loop and remove the array and nut out how to add this temp dict to the main dict im cluless as to that too but im sure ill be able to find something somewhere, would you $$anonymous$$d showing me as the forEach loop as you said i could go with i tried adding a for each loop in and it was just getting the same error again cos it was manipulating the temporary dictionary

sorry im a total noob at all this i realy appreciate your time

capture.png (12.6 kB)
an-bg.png (14.1 kB)
avatar image scradsleep scradsleep · May 17, 2020 at 07:17 AM 0
Share

hello me again now have this (thanks to your help)

public void Remove() { List selectedObjects = playerController.GetSelected();

          foreach (SelectedInfo unit in selectedObjects)
          {
             var items = new Dictionary<Component[], Component>(groupLookupCache);
             {
                foreach (var item in items)
                {
                   if (groupLookupCache.TryGetValue(item.Key, out Component value))
                   {
                      for (int j = 0; j < item.Key.Length; j++)
                      {
                         if (unit.isSelected == true)
                         {
                            groupLookupCache.Remove(item.Key);
                         }
                      }
                   }
                }
             }
          }
       }



only problem is that if statement on the end aint working and its removing the whole group.... could be a feature for now i dont $$anonymous$$d at leaset i can now group and degroup the units, might come back to that as a polish thing so that only what you have selected gets removed. no biggie

thankyou for everything my friend ill immortalise you as an npc im thinking a robot with the name like @AlirezaSH2004 "bleep bloop" tyvm!!!

avatar image AlirezaSH2004 scradsleep · May 17, 2020 at 12:13 PM 0
Share

Hello there. I’m happy that I’ve helped. Thanks bro, my username says 2004, cause I’ve been born in 2004, that’s not some arbitrary stuff (Grinning Face With Sweat). Good luck with the rest of your project.

Show more comments
avatar image AlirezaSH2004 · May 17, 2020 at 06:04 AM 1
Share

As you’re comfortable with foreach statement:

 foreach (SelectedInfo unit in selectedObjects)
           {
              if (unit.isSelected)
              {
                    var tc = new Dictionary<Component[], Component>(groupLookupCache);
                    foreach (var item in tc)
                    {
                       for (int i = 0; i < item.Key.Length; i++)
                       {
                          this.groupLookupCache.Remove(item.Key);
                       }
                    }
                 }
              }

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

128 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

Related Questions

dictionary and for each loop and for loop frustrating bug 1 Answer

Which is a faster find? (foreach) or (for-loop) 2 Answers

For loop update variables value before functions complete 2 Answers

Generic dictionary emptying itself? 0 Answers

Get child position from last gameobject in array 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