Dictionary ContainsKey not working with Vector3s?
The screenshot summarizes my problem. I have a Vector3 array corners, which is initialized as six distinct positions (which happen to be the WorldPositions of a Hexagons vertices). I also have a Dictionary Corners. For now, the HexCorner class is irrelevant. After assigning the corners to the array (which has length 6), I iterate i 0 to 6. What I am doing inside the loop can be seen in the image. In summary, if the corner has already been added, I set the value in the hexCorners array (an array of HexCorners] to be the value in the dictionary. Otherwise, I create a new HexCorner, assign it to the hexCorners array, and add it to the dictionary. This was not giving me the expected results (I noticed that there appeared to be duplicates in the dictionary, but I wasn't getting any errors).
After debugging, I saw this following bewildering thing in the watches. the Corners.Keys obviously contains the value of corners[i] (both of which are visible in the watches as the same value). However, Corners.ContainsKey(corners[i]) returns false, and the if statement in the code has been skipped over (excluding an unusual green arrow).
I suspect the problem has something to do with Vector3s being structs (but I assumed that they worked in Dictionaries). Note that I am not getting any errors when I try to add the values to the dictionary, and they do show up as duplicates in the watches.
Let me know if there's anything else you want me to post.
if one is (-34.64, ...) and the other is (-34.63, ...) they won't be considered equal but their toString methods will print the same output, the default equals is true if the magnitude of their difference is less than 1e-5. If that is too accurate, you could give your dictionary a custom implementation of IEqualityComparer<Vector3>