C
C#16mo ago
GentleMads

❔ Dictionary won't be larger than one

Hello i'm trying to create a gridmap where every tile has some info, which i use class for storing, but when i try to add new tile information to my dictionary it just limits it to one while only showing the latest key and value This is my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
using System;

public class Controls : MonoBehaviour
{
public class myResource {
public string Name { get; set; }
public int Amount { get; set; }
}

public Tilemap mainMap;
public Tilemap objectMap;
public Tilemap resourceMap;
public TileBase testtile;
// public Dictionary<Vector3Int, string> objectPlacements;
// public Dictionary<Vector3Int, string> resource;

public Dictionary<Vector3Int, myResource> tiles;

private Rigidbody2D rb;
public float speed = 10f;
// Start is called before the first frame update
void Start()
{

rb = GetComponent<Rigidbody2D>();
}

void Update()
{
if (Input.GetMouseButtonDown(0)) {
resourceMap.SetTile(GetWorldCellPos(), testtile);
tiles = new Dictionary<Vector3Int, myResource>();
var tile = new myResource {
Name = "copper",
Amount = UnityEngine.Random.Range(1,10)
};
tiles.Add(GetWorldCellPos(), tile);
Debug.Log(tiles[GetWorldCellPos()].Name);
Debug.Log(tiles[GetWorldCellPos()].Amount);
}
if (Input.GetMouseButtonDown(1)) {
if (tiles.ContainsKey(GetWorldCellPos())) {
Debug.Log(tiles[GetWorldCellPos()].Name);
Debug.Log(tiles[GetWorldCellPos()].Amount);
}
foreach (KeyValuePair<Vector3Int, myResource> item in tiles) {
Debug.Log("Key: " + item.Key + " Value: " + item.Value);
}
Debug.Log(tiles.Count);

}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
using System;

public class Controls : MonoBehaviour
{
public class myResource {
public string Name { get; set; }
public int Amount { get; set; }
}

public Tilemap mainMap;
public Tilemap objectMap;
public Tilemap resourceMap;
public TileBase testtile;
// public Dictionary<Vector3Int, string> objectPlacements;
// public Dictionary<Vector3Int, string> resource;

public Dictionary<Vector3Int, myResource> tiles;

private Rigidbody2D rb;
public float speed = 10f;
// Start is called before the first frame update
void Start()
{

rb = GetComponent<Rigidbody2D>();
}

void Update()
{
if (Input.GetMouseButtonDown(0)) {
resourceMap.SetTile(GetWorldCellPos(), testtile);
tiles = new Dictionary<Vector3Int, myResource>();
var tile = new myResource {
Name = "copper",
Amount = UnityEngine.Random.Range(1,10)
};
tiles.Add(GetWorldCellPos(), tile);
Debug.Log(tiles[GetWorldCellPos()].Name);
Debug.Log(tiles[GetWorldCellPos()].Amount);
}
if (Input.GetMouseButtonDown(1)) {
if (tiles.ContainsKey(GetWorldCellPos())) {
Debug.Log(tiles[GetWorldCellPos()].Name);
Debug.Log(tiles[GetWorldCellPos()].Amount);
}
foreach (KeyValuePair<Vector3Int, myResource> item in tiles) {
Debug.Log("Key: " + item.Key + " Value: " + item.Value);
}
Debug.Log(tiles.Count);

}
3 Replies
Angius
Angius16mo ago
Well, on every Update() you replace the dictionary with a new, empty one Then you add one item
GentleMads
GentleMads16mo ago
Ffs I have wasted so much time I'm so blind, i really really appreciate your help.
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.