- Home /
Using Dictionaries to store and retrieve GameObjects?
Ive been working on a isometric grid where buildings are GameObjects stored as values in a dictionary with a vector2 (coordinates) as a key. A newly instantiated object is added into the dictionary with the key of its coordinates under IsOccupied.
GameObject InstantiatedObject = Instantiate(finalObject, transform.position, Quaternion.identity);
if(gridDataOccupied.ContainsKey(new Vector2Int(cp.x, cp.y)))
{
gridDataOccupied[new Vector2Int(cp.x,cp.y)] = InstantiatedObject;
}
else
{
gridDataOccupied.Add(new Vector2Int(cp.x,cp.y), InstantiatedObject);
}
This code takes instantiates selected object (finalObject) and lists it in the grid coordinates in the dictionary at the cursors position (transform.position) onto the nearest grid under the cursor (cp)
b_brush.GetComponent<Brush>().gridDataOccupied[new Vector2Int(cpb.x +1, cpb.y)].GetComponent<HouseBehaviour>()
This code reaches to a cell beside it and checks if it has the script . This and it works. So i know getting GameObjects from dictionaries work.
Destroy(b_brush.GetComponent<Brush>().gridDataOccupied[new Vector2Int(cpb.x +1, cpb.y +1)]);
this code destroys the object. It produces no errors, but doesn't do anything.
b_brush.GetComponent<Brush>().gridDataOccupied[new Vector2Int(cpb.x +1, cpb.y +1)].GetComponent<HouseBehaviour>().Level == Level
the above checks if the house is the same level as a nearby house. It produces no errors also, but doesn't do anything.
b_brush.GetComponent<Brush>().gridDataOccupied[new Vector2Int(cpb.x +1, cpb.y +1)].GetComponent<HouseBehaviour>().is2x2 == true
Getting the bool from the script as stated above produces an error (i dont know why or what to do about it). So it doesn't work. Im assuming it's not working because its creating a copy of it when i use this code rather than getting the origional. If that's the case, how do i get it to get the origional? Why am I getting an error when I try to get is2x2? Any help would be greatly appreciated as I am a compleate noob at programming and all of this is new to me. Thanks.
Answer by andrew-lukasik · Apr 15, 2021 at 03:18 PM
Use your gridDataOccupied
Dictionary
as it's name suggests - as a valid info on grid occupancy. So when filling a cell, always make sure nothing is there already, otherwise you're just replacing that object while loosing it's reference:
var xy = new Vector2Int( cp.x , cp.y );
if( gridDataOccupied.ContainsKey(xy) )
{
Debug.Log($"Negative. {xy} is occupied",gridDataOccupied[xy]);
return false;
}
else
{
GameObject instantiated = Instantiate( finalObject , transform.position , Quaternion.identity );
gridDataOccupied.Add( xy , instantiated );
Debug.LogError($"{xy} is occupied by {instantiated.name} now.",instantiated);
return true;
}
And, naturally, once you destroy a GameObject
- make sure gridDataOccupied
is updated on that too:
var xy = new Vector2Int( cp.x , cp.y );
if( gridDataOccupied.ContainsKey(xy) )
{
var go = gridDataOccupied[xy];
if( go!=null ) Destroy( go );
else Debug.LogError($"You messed something up. gridDataOccupied[ {xy} ] is null");
gridDataOccupied.Remove( xy );
return true;
}
else
{
Debug.LogError($"Negative. {xy} is not even occupied.");
return false;
}
When it comes to your question. Rewrite this line:
gridDataOccupied[new Vector2Int(cpb.x +1, cpb.y +1)].GetComponent<HouseBehaviour>().is2x2 == true
As:
var xy = new Vector2Int( cpb.x+1 , cpb.y+1 );
var go = gridDataOccupied[xy];
if( go==null ) Debug.LogWarning($"gameobject under {xy} coordinate is null.");
var houseComp = go.GetComponent<HouseBehaviour>();
if( houseComp==null ) Debug.LogWarning($"{go.name} has no HouseBehaviour component",go);
Debug.Log($"house.is2x2: {houseComp.is2x2} ",go);
And answer surely reveal itself in any Console window nearby.
I debugged and figured out the probolem. The part of my code that handles housing was accessing the prefab rather than the instance because I was assigning the grid finalObject
rather than InstantiatedObject
. Thanks a bunch for the help.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Camera rotation around player while following. 6 Answers
Does it make any difference whether position is an integer or a float? 2 Answers
Make something arrive at position in exactly X seconds? 2 Answers
How to Set Animation Position on Slope (animation) help please 1 Answer