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 CrashCZ · Jan 13, 2018 at 05:15 PM · listsdictionaryvaluesarraylisttypes

How to add dictionary keys with defined values to a list

I am trying to add dictionary keys with player-defined values into a list, for example if playertypes 1 into input field, the programm will add into the list all keys with value 1, but i can't figure out how

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 Orokon · Jan 13, 2018 at 05:38 PM

Get the input in the code and use its .text value. With that value you can now get elements out of a dictionary like:

 Dictionary<string, GameObject> dictionary = new Dictionary<string, GameObject>();
 InputField input = FindObjectOfType<InputField>();
 input.text = "foo";
 GameObject elementFromDictionary = dictionary[input.text];

If you want to populate a dictionary by player defined key and value then get the values and add it like this:

 Dictionary<string, GameObject> dictionary = new Dictionary<string, GameObject>();
 
 string key = "foo"; // Get this from whatever the player enters
 GameObject foo = new GameObject(); // This is the userdefined value
 
 dictionary.Add(key, foo); // pass the key and the stuff you want to have in the dictionary

Comment
Add comment · Show 4 · 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 CrashCZ · Jan 13, 2018 at 05:50 PM 0
Share

I'm sorry but this doesn't help me. I already have values and keys stored in Dictionary. What I want to know is how to put it's keys that have the user defined value into a list

avatar image Orokon CrashCZ · Jan 13, 2018 at 05:55 PM 0
Share

Okay that clears things up:

 Dictionary<string, int> dictionary = new Dictionary<string, int>();
 dictionary.Add("foo", 10);
 dictionary.Add("bar", 20);
 dictionary.Add("baz", 10);
 
 List<string> listWith$$anonymous$$eys = new List<string>();
 
 foreach ($$anonymous$$eyValuePair<string, int> dictionaryItem in dictionary) {
     if (dictionaryItem.Value == 10) {
         listWith$$anonymous$$eys.Add(dictionaryItem.$$anonymous$$ey);
     }
 }
avatar image Orokon Orokon · Jan 13, 2018 at 05:57 PM 0
Share

In this case the value 10 is what the user typed into the input.

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

74 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

Related Questions

Getting the sum of random values in the dictionary 2 Answers

How can you do calculations on two lists? 1 Answer

Can't assign dictionary values 0 Answers

Adding elements to a list is changing the initial values. Need help! 0 Answers

List of bools assigned to each Gameobject in List/Array 0 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