- Home /
Dictionary Function not responding to Key Calls
public Dictionary<Hex, Hex> GetDestinations(Hex start, int depth, int team){
Queue<Hex> frontier = new Queue<Hex>();
frontier.Enqueue(start);
Dictionary<Hex, Hex> cameFromSearch = new Dictionary<Hex, Hex> ();
cameFromSearch.Clear ();
cameFromSearch[start] = null;
for (int k = 0; k < depth; k++) {
Queue<Hex> fringe = new Queue<Hex>();
foreach(Hex current in frontier){
for(int i = 0; i < 6; i++) {
Hex next = current.Neighbor(map.GetDirection(i));
if (!cameFromSearch.ContainsKey(next) && map.ContainsKey(next) && map.GetTile(next).GetComponent<GameTile>().Passable) {
if(team == 1){
map.GetTile(next).transform.Find ("Reticle").Find ("MO_GEN_HexReticle_001").gameObject.GetComponent<Renderer> ().material.SetColor ("_EmissionColor", Color.cyan);
map.GetTile(next).transform.Find ("Reticle").gameObject.SetActive(true);
}
fringe.Enqueue(next);
cameFromSearch[next] = current;
}
}
}
frontier = fringe;
}
return cameFromSearch;
}
So I am creating a dictionary, so that I can create a path, but when I do this (from a different class)
Dictionary<Hex, Hex> tilesInRange = gameMain.GetComponent<Controller>().GetDestinations(currentTile, 4, Team);
foreach (Hex tile in tilesInRange.Values) {
Debug.Log(tile.Q + ", " + tile.R);
}
it throws a nullreferenceexcepton ..
am I missing something?
Is the if (!cameFromSearch.Contains$$anonymous$$ey(next) && map.Contains$$anonymous$$ey(next) && map.GetTile(next).GetComponent().Passable) line ever evaluating as true?
even more bizzare, if I access a specific key, it seems to work, ie
Debug.Log (tilesInRange[new Hex(14, -3)].Q + ", " + tilesInRange[new Hex(14, -3)].R);
Ah, it might be:
foreach($$anonymous$$eyValuePair<Hex, Hex> entry in tilesInRange)
$$anonymous$$eyValuePair didn't work either .. really strange.
What line is the null reference on? If it is on
Debug.Log(tile.Q + ", " + tile.R);
then you might be adding {Hex, null} to your dictionary in which case using null.Q or null.R would trigger a null reference exception. Try
foreach (Hex tile in tilesInRange.Values) {
if(tile!=null)
Debug.Log(tile.Q + ", " + tile.R);
else
Debug.Log("Error Hex is null");
}
Your answer
Follow this Question
Related Questions
access GameObject only when instantiated 1 Answer
Null Reference Expantion 1 Answer
Testing if object is null creates a NullReferenceException. 1 Answer
Missing Reference after null Check 4 Answers
Unity null check 1 Answer