- Home /
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 ()
{
}
}
Your answer
Follow this Question
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