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 post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Ganu132 · Apr 13, 2015 at 07:44 PM · c#classdictionary

Problem with dictionary inside of a class

Hello, I just began working with classes and I cant get variable Dictionary blocks to work. Whole script manages terrain generation. Im trying to create chunks and assign them to dictionary - that works, inside of that chunk there is another dictionary containing all the blocks of chunk - it just doesnt want to store newly created blocks in it. When Im trying to access specific block by:

GameObject tt = chunks[new Vector2(3,2)].blocks[new Vector2(3,2)];
Debug.Log(tt.transform.position);

It gives me this :/

KeyNotFoundException: The given key was not present in the dictionary. System.Collections.Generic.Dictionary`2[UnityEngine.Vector2,scrTerrainManager+chunk].get_Item (Vector2 key) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:150) scrTerrainManager.Start () (at Assets/Scripts/mapGenMoj/scrTerrainManager.cs:52)

Please help me Im almost total newb in that matter...

 using System;
 using System.Collections.Generic;
 using UnityEngine;
 using System.Collections;
 
 public class scrTerrainManager : MonoBehaviour 
 {
     public class chunk
     {
         //class variables
 
         public int chunkSize;
         public Dictionary<Vector2, GameObject> blocks = null;
             public chunk(int xPos, int zPos, int size)
             {
                 chunkSize = size;
                 blocks = new Dictionary<Vector2,GameObject>();
                 
                 GameObject chunk = new GameObject("chunk," + xPos + "," + zPos);
                 chunk.transform.position = new Vector3(xPos * chunkSize, 0, zPos * chunkSize);
 
                 for(int x = 0; x < chunkSize; x++)
                 {
                     for(int z = 0; z < chunkSize; z++)
                     {
                         GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube) as GameObject;
                         cube.transform.parent = chunk.transform;
                         cube.transform.localPosition = new Vector3(x, 0, z);
                         GameObject tt = cube;
                         blocks[new Vector2(x, z)] = tt;
                     }
                 }
             }
     }
 
     public int mapSize = 5;                                                                     //how big is the square made of chunks
     public int chunkSize = 8;                                                                    //chunk size in blocks
 
     Dictionary<Vector2, chunk> chunks = new Dictionary<Vector2, chunk>();
 
     void Start () 
     {
         for(int x = -2; x < 3; x++)
         {
             for(int z = -2; z < 3; z++)
             {
                 //SpawnChunk(x, z, chunkSize);
                 chunks[new Vector2(x, z)] = new chunk(x, z, chunkSize);
             }
         }
             // this gives error every time
         GameObject tt = chunks[new Vector2(3,2)].blocks[new Vector2(3,2)];
         Debug.Log(tt.transform.position); // just debug thingy
     }
 
     chunk SpawnChunk (int x, int z, int size) // ignore it, obsolete 
     {
         chunk thisChunk = new chunk(x, z, size);
         return thisChunk;
         /*
         GameObject chunk = new GameObject("chunk," + x + "," + z);
         chunk.transform.position = new Vector3(x * chunkSize, 0, z * chunkSize);
         chunk.AddComponent<scrChunk>();
         chunks[new Vector2(x,z)] = chunk;
         */
     }
     void DeSpawnChunk()
     {
 
     }
     void Update () 
     {
         
     }
 }
 
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

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

find prefabs that extend Base Class c# 0 Answers

Call CSharp class from JS Error- Dumbfounded 3 Answers

CSharp Classes = Scripts? Difference between private string and normal string? 1 Answer

Cannot use custom type as key for dictionary 0 Answers

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


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