• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

List of bools assigned to each Gameobject in List/Array 0 Answers

How do I create a Key and Values Dictionary array in C# 1 Answer

How to have Database of Dictionary elements with online images??? 0 Answers

Combine Children Dictionary in place of Hashtable? 2 Answers

How do I create a Key and Values Dictionary array in C# 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges