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 /
This question was closed Nov 22, 2020 at 02:05 PM by Bobnotarobot for the following reason:

Other

avatar image
0
Question by Bobnotarobot · Nov 24, 2020 at 04:00 PM · arraysdictionarykeys

unity dictionary with array as keys causes problems

I am using a dictionary to store coordinates and their respective heights to generate random terrain using a diamond square algorithm. However, when I try accessing keys of the dictionary, I keep getting "KeyNotFoundException: The given key was not present in the dictionary.". Here is the code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Tilemaps;
 
 public class TerrainFractal : MonoBehaviour
 {
     private int SizePower = 3;
     private int height = 0;
     private int width = 0;
     private float seed = 0f;
     private float randomHeight = 0;
 
     Dictionary < List<int>, float> WorldMap = new Dictionary< List<int>, float>();
 
     // Start is called before the first frame update
     void Start()
     {
         height = Mathf.RoundToInt(Mathf.Pow(2, SizePower)) + 1;
         width = height;
         GenerateFractalMap();
     }
 
     public void GenerateFractalMap() {
         for (int x = 0; x < width; x ++ ) {
             for (int y = 0; y < height; y ++ ) {
                 var coordinates = new List<int> {x, y};
                 WorldMap[coordinates] = 0;
             }
         }
         
         randomHeight = Random.Range(0f, 10f);
         WorldMap[new List<int> {0, 0}] = randomHeight;
         WorldMap[new List<int> {0, height - 1}] = randomHeight;
         WorldMap[new List<int> {width - 1, 0}] = randomHeight;
         WorldMap[new List<int> {width, height - 1}] = randomHeight;
         for (int i = 0; i < 1; i++ ) {
             DiamondStep(i);
             Debug.Log(WorldMap);
         }
     }
 
     public void SquareStep(int SizePower, Dictionary <int[], float> Map, int itteration) {
         int numberOfPoints = Mathf.RoundToInt(Mathf.Pow(2, itteration));
         int NumberOfLines = numberOfPoints * 2 - 1;
         int coordX = 0;
         int coordY = 0;
     }
 
     public void DiamondStep(int itteration) {
         int numberOfPoints = Mathf.RoundToInt(Mathf.Pow(2, itteration));
         int NumberOfLines = numberOfPoints;
         int coordX = 0;
         int coordY = 0;
         var coords = new int[2];
         int displacement = Mathf.RoundToInt(Mathf.Pow(2, SizePower - itteration - 1));
         int multiple = Mathf.RoundToInt(Mathf.Pow(2, SizePower - itteration));
         float newHeight = 0;
         float randomDisplacement = 0;
         for (int l = 0; l < NumberOfLines; l ++) {
             coordX = displacement + l * multiple;
             for (int p = 0; p < numberOfPoints; p ++) {
                 coordY = displacement  + p * multiple;
                 Debug.Log(WorldMap[new List<int> {0,0}]);
                 newHeight += WorldMap[new List<int> {coordX - displacement, coordY - displacement}];
                 newHeight += WorldMap[new List<int> {coordX - displacement, coordY + displacement}];
                 newHeight += WorldMap[new List<int> {coordX + displacement, coordY - displacement}];
                 newHeight += WorldMap[new List<int> {coordX + displacement, coordY + displacement}];
 
                 newHeight = newHeight / 4;
                 randomDisplacement = Random.Range(-1f, 1f);
                 newHeight += randomDisplacement;
                 WorldMap[new List<int> {coordX, coordY}] = newHeight;
             }
         }
     }
     // Update is called once per frame
     void Update()
     {
 
     }
 }

The error occurs at line 66: Debug.Log(WorldMap[new List {0,0}]); I have checked, and the dictionary seems to contain a key of the form [0, 0], so this leaves me very confused.

Thank you for your help.

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

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

140 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to get the keys of a SimpleJSON dictionary ? 3 Answers

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

Help Sorting large class full of variables.,Creating and array of arrays of arrays of variables with different data types. 1 Answer

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