Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 scradsleep · May 16, 2020 at 10:37 PM · listbooleanfor-loopforeachdictionary

dictionary and for each loop and for loop frustrating bug

hey all, hope your all keeping well in these times, i have run into a little snag with my code, i am trying to get a key(S) from a dictionary and on each one of those keys is a component with a bool that i am trying to set true, the code is working as intended but its only setting one of the bool to true not say all 4 if i have 4 in the dictionary

       public void OnMouseDown()
       {
          if (Input.GetMouseButtonDown(0))
          {
            foreach (var item in groupLookupCache)
            {
                if (groupLookupCache.TryGetValue(key, out Component value))
                {
                   for (int i = 0; i < key.Length; i++)
                   {
                     key[i].GetComponent<SelectedInfo>().isSelected = true;
                   }
                }
            }
          }
       }

this is the latest and greatest i have tried to come up with and to my understanding of the language is that would work but its not its only setting one of the bool to true, any help would be appreciated if someone could please help by pointing me in the right direction it would be ace as i have hit a brick wall and i fear i may have to shelve the whole project if i cannot get it to work as it is one of the main features that i am trying to achieve. please and thank-you!!

brad

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 16, 2020 at 11:01 PM

Hello there. Probably you're trying to apply this to all of the items in "groupLookupCache", so, as you have done this in your code, you've not implemented it in your if statement after the foreach statement. For this to work, you should've written:

 public void OnMouseDown()
        {
           if (Input.GetMouseButtonDown(0))
           {
             foreach (var item in groupLookupCache)
             {
                 if (groupLookupCache.TryGetValue(item.Key, out Component value))
                 {
                    for (int i = 0; i < item.Key.Length; i++)
                    {
                      item.Key[i].GetComponent<SelectedInfo>().isSelected = true;
                    }
                 }
             }
           }
        }

Comment
Add comment · Show 3 · 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 16, 2020 at 11:15 PM 0
Share

@AlirezaSH2004 thankyou i tried what you said and i get this error

alt text

capture.png (15.6 kB)
avatar image scradsleep scradsleep · May 16, 2020 at 11:27 PM 0
Share

capital 'k'

gotcha

thanks a bunch

you a legend @AlirezaSH2004

avatar image AlirezaSH2004 · May 16, 2020 at 11:42 PM 1
Share

YW brother. Hope you have some good times with your project.

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 to have a loop run itself again if returned value is a known exception, with a new return value. 1 Answer

For loop does not loop C# 2 Answers

why isn't my lock working 1 Answer

A node in a childnode? 1 Answer

Copy values between two classes in two lists. 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