GentleMads
Explore posts from servers❔ 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);
}
7 replies