Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Guy_Yome · May 14, 2015 at 06:29 PM · floatdictionaryvaluesassignincrement

Can't assign dictionary values

Hello! I have a little problem here. I searched how to do it. But I didn't come with something good. So, I'm trying to assign values in a "complex" dictionary, but I can't seem to do it. The error says : KeyNotFoundException : The key given was not present in the dictionary.

         dimensionsCameras = new Dictionary<int, Dictionary<int, float[]>>();
         dimensionsCameras[0][0] = new float[] { 0f, 0f, 1f, 1f }; // 1 joueur - joueur 1
         dimensionsCameras[1][0] = new float[] { 0f, 0.5f, 0.9f, 0.5f }; // 2 joueurs - joueur 1
         dimensionsCameras[1][1] = new float[] { 0.1f, 0f, 0.9f, 0.5f }; // 2 joueurs - joueur 2

Comment
Add comment · Show 1
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 fafase · May 14, 2015 at 06:36 PM 0
Share

This is a dictionary not a jagged array. The purpose of dictionary is the key-value pair. So it means you know the key and want the value. Considering a [i][j] indexing is kinda wrong as you cannot know where the items are stored. I don't even think indexing is possible like this.

1 Reply

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

Answer by DoTA_KAMIKADzE · May 14, 2015 at 06:32 PM

The error says what it says - you can access (get/set) in that manner only existing keys, firstly you'll need to populate your dictionary, like so:

     dimensionsCameras = new Dictionary<int, Dictionary<int, float[]>>();
     Dictionary<int, float[]> nestedDict = new Dictionary<int,float[]>();
     nestedDict.Add(0, new float[] { 0f, 0f, 1f, 1f });
     dimensionsCameras.Add(0, nestedDict);
     //next time you'll be able to set or get it like dimensionsCameras[0][0] because Key 0 Key 0 already exists
     //you can also use if statements to check if key exists, e.g.:
     if (dimensionsCameras.ContainsKey(0)) //might be useful if you're not initializing it all in one go and can add few more
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 Guy_Yome · May 14, 2015 at 08:02 PM 0
Share

Answers are quick here! And thanks. It worked like a charm altought it's quite a compact code.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Dictionary doesn't exist? 1 Answer

How do I assign the velocity on the "y" axis to a float? 1 Answer

How to get accurate values from curves using Mecanim 0 Answers

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

Assigning a value to a Dictionary key changes other key values 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