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 AppleseedSmoothie · Sep 12, 2018 at 05:19 AM · listdictionary

Why can't Dictionaries have the same variables

When the game first all the highscore points are set to 0 and "empty". I'm using a dictionary to keep the score and name together so how come when I put it into a for statement it doesn't like the variables being the same? And since it happens would it be best to use a dictionary especially if two scores end up being exactly the same?

  for(int i = 0; i < 6; i++)
             {
                 highscoreList.Add(0, "empty");
             }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Atiyeh123 · Sep 12, 2018 at 06:00 AM

HI, as the C# documation said , you can't add duplicate keys in dictionaries.all keys should be unique. link text

if you want duplicate keys you can use list's of keyvaluepairs. like this:

 List<KeyValuePair<string, string>> myduplicateLovingDictionary= new List<KeyValuePair<string, string>>();
 KeyValuePair<string,string> myItem = new KeyValuePair<string,string>(dialedno, line);
 myduplicateLovingDictionary.Add(myItem);
Comment
Add comment · Show 1 · 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 AppleseedSmoothie · Sep 12, 2018 at 06:04 AM 0
Share

Ahh fantastic, every time I googled for double valued lists it'll always show something about Dictionaries or Classes or Arrays but this is more helpful

avatar image
0

Answer by maxoja · Sep 12, 2018 at 06:21 AM

@LemonCore56 There are some ways to update add/update/read values in a dictionary. This might be the simplest form that you can retry in my opinion.


And to answer your question "why you cannot call Add(0,"empty") multiple times", it is not allowed is because dictionary data structure does not allow you to add key-value pairs with duplicated keys. To demonstrate, if you have a contact list in your phone and you created 2 contacts with different numbers but you named both of them as "Adam", the next time when you ask your friend to find Adam's number, your lucky friend will of course get confused whether which Adam you are looking for.


However, in your use case of dictionary, I suppose you can update it using the following code.


 Dictionary<string, int> dict = new Dictionary<string, int>();

 //initialize key-value pairs
 dict["score"] = 0;  //key "score", initial value 0
 dict["lives"] = 3;  //key "lives", initial value 3

 //update
 dict["score"] = 100;
 dict["lives"] -= 1;



I wish this helps!

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

92 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

Related Questions

A node in a childnode? 1 Answer

Frame Drop on a function dealing with an updated List type (Only once after new element Added) 0 Answers

List of Different Classes That All Inherit From One Class 1 Answer

Changing Custom Object Variable 1 Answer

I want to make a search engine, is a dictionary the right choice? 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