I have a Dictionary containing positions that correspond to block values that I am using to get the current blocks the player is over (as an alternative to having each individual block have its own BoxCollider2D and raycasting, which may or may not be better, I'm not sure)
In the Update method, I check for a specific blocks position, to which it returns true.
void Update()
{
if (BlocksDictionary.ContainsKey(new Vector2(-2.56f, 6.4f)))
print("Value exists");
}
However, in the GetBlocksAtPosition method, when trying to check for the same position, it returns false.
public Dictionary<Block, SpriteRenderer> GetBlocksAtPosition(float x, float y, bool gridifyCoords = false)
{
float newX = x;
float newY = y;
if (gridifyCoords)
{
Vector2 gridCoords = new Vector2(Mathf.Round(x / 1.28f) * 1.28f, Mathf.Round(y / 1.28f) * 1.28f);
newX = gridCoords.x;
newY = gridCoords.y;
}
Vector2 blockCoords = new Vector2(newX, newY);
if (BlocksDictionary.ContainsKey(blockCoords))
return BlocksDictionary[blockCoords];
else
{
print("Blocks not found at: " + newX + " - " + newY);
return null;
}
}
And here's how I am adding blocks to the dictionary.
if (BlocksDictionary.ContainsKey(blockPos))
{
if (!BlocksDictionary[blockPos].ContainsKey(blockSO))
BlocksDictionary[blockPos].Add(blockSO, spriteRenderer);
}
else
{
Dictionary<Block, SpriteRenderer> newBlockDict = new Dictionary<Block, SpriteRenderer>();
newBlockDict.Add(blockSO, spriteRenderer);
BlocksDictionary.Add(blockPos, newBlockDict);
}
It's pretty clear to me that the position value in the dictionary exists, so I don't understand how the GetBlocksAtPosition method doesn't find them.
The only things I've really tried was print statements, since I have no other ideas as to what could be happening.