List.Find() not returning any results

Hello,
I am trying to find a hex within a chunk. The query is structured like this:

//within a loop through each hex
Chunk.Hex thisAboveHex = chunk.chunkHexes.Find(h => h.hexName == $"Hex_{hz}_{hx}_{hy + 1f}");
Transform thisAboveHexObj;
Debug.Log($"search for 'Hex_{hz}_{hx}_{hy + 1f}' in '{chunk.chunkName}'");

try { thisAboveHexObj = thisAboveHex.hexObj.transform; Debug.Log("GOOD"); }
catch { thisAboveHexObj = null; Debug.Log("NULL"); };

if (thisAboveHexObj && thisAboveHexObj.gameObject.activeSelf)//if there exists an active part above this part
{
  bool setInactive = true;
  Debug.Log("FOUND AND ACTIVE!!!!");
}

I’ve attached a picture of the output. “FOUND AND ACTIVE!!!” is never printed. The reason why I use try {} catch {} as opposed to just testing thisAboveHex within the if () statement is because thisAboveHex is of type Chunk.Hex which cannot be tested as a boolean in conditionals, so I did a work-around making thisAboveHexObj and testing for that object’s existence.
Why doesn’t .Find() return a Chunk.Hex value when it clearly exists in the List chunk.chunkHexes? chunk is an object of type Chunk, and .chunkHexes is of type List<Chunk.Hex>. Is there some sort of type mixup using .Find()? I’ve attached a picture to more clearly demonstrate this.
Any direction would be appreciated. Thanks in advance!

Modify your search predicate on line 2 to output every h.hexName considered and see what they look like, maybe write them to a file.

1 Like

Thank you, Kurt! You helped me identify the problem. It was rendering the map from bottom to top, and as a result, hy + 1 never existed.

1 Like