Dictionary doesn't find value when using new object as key

I am trying to build a Dictionary of PathNodes keyed by NodeCoordinates. I have overridden GetHashCode in NodeCoordinate and it should return a constant unique value. If I loop through the keys of the dictionary, the lookup works fine, but if I create a new NodeCoordinate with the coordinates of a PathNode that should exist, the lookup fails even though the hash codes are equal.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class PathNode : MonoBehaviour {

    public static Dictionary<NodeCoordinate, PathNode> pathNodes;

    public PathNode left;
    public PathNode right;
    public PathNode forward;
    public PathNode backward;

    private NodeCoordinate coord;

    void Awake()
    {
        if (PathNode.pathNodes == null)
        {
            PathNode.pathNodes = new Dictionary<NodeCoordinate, PathNode>();
        }

        NodeCoordinate coord = new NodeCoordinate(transform.position.x, transform.position.z);
        this.coord = coord;
        PathNode.pathNodes.Add(coord, this);

        NodeCoordinate leftChord = new NodeCoordinate(coord.x - 1, coord.z);
        NodeCoordinate rightChord = new NodeCoordinate(coord.x + 1, coord.z);
        NodeCoordinate forwardChord = new NodeCoordinate(coord.x, coord.z + 1);
        NodeCoordinate backwardChord = new NodeCoordinate(coord.x, coord.z - 1);

        if (PathNode.pathNodes.ContainsKey(leftChord))
        {
            this.left = PathNode.pathNodes[leftChord];
            this.left.right = this;
        }
        if (PathNode.pathNodes.ContainsKey(rightChord))
        {
            this.right = PathNode.pathNodes[rightChord];
            this.right.left = this;
        }
        if (PathNode.pathNodes.ContainsKey(forwardChord))
        {
            this.forward = PathNode.pathNodes[forwardChord];
            this.forward.backward = this;
        }
        if (PathNode.pathNodes.ContainsKey(backwardChord))
        {
            this.backward = PathNode.pathNodes[backwardChord];
            this.backward.forward = this;
        }
    }

    private static bool debug = true;

    void Update()
    {
        if (debug)
        {
            foreach (NodeCoordinate coord in PathNode.pathNodes.Keys)
            {
                Debug.Log(coord + " : " + PathNode.pathNodes[coord] + " : " + coord.GetHashCode());
            }

            foreach (PathNode node in PathNode.pathNodes.Values)
            {
                NodeCoordinate leftChord = new NodeCoordinate(node.coord.x - 1, node.coord.z);
                PathNode leftNode;
                Debug.Log("Left: " + leftChord + " : " + PathNode.pathNodes.TryGetValue(leftChord, out leftNode) + " : " + leftChord.GetHashCode());
            }

            debug = false;
        }
    }
}

public class NodeCoordinate
{
    public float x;
    public float z;

    public NodeCoordinate(float x, float z)
    {
        this.x = x;
        this.z = z;
    }

    public bool Equals(NodeCoordinate coord)
    {
        return (this.x == coord.x && this.z == coord.z);
    }

    public override int GetHashCode()
    {
        return string.Format("{0}x{1}", this.x, this.z).GetHashCode();
    }

    public override string ToString()
    {
        return "Coordinate: " + this.x + " x " + this.z;
    }
}

This is the output from my little debug:

74742-debug.png

As you can see, when looping through the keys, the lookups with hashcodes 2137067561 and 1824497336 work, but when I instantiate a new NodeCoordinate and try to look it up, it has the same hashcode but the lookup fails.

So for some reason, when I add a IEqualityComparer with the exact same logic, it works.

Changed the declaration of the dictionary to this:

if (PathNode.pathNodes == null)
        {
            PathNode.pathNodes = new Dictionary<NodeCoordinate, PathNode>(new NodeCoordinateComparer());
        }

And added this:

public class NodeCoordinateComparer : IEqualityComparer<NodeCoordinate>
{
    public bool Equals(NodeCoordinate a, NodeCoordinate b)
    {
        return (a.x == b.x && a.z == b.z);
    }

    public int GetHashCode(NodeCoordinate coord)
    {
        return string.Format("{0}x{1}", coord.x, coord.z).GetHashCode();
    }


}

It’s total overkill to use a class for this. Use a struct. If two structs are compared the compiler automatically compares it’s content. I’m also wondering why you actually create a seperate class / struct for this. Why don’t you simply use Vector2?

Keep in mind that comparing float values can lead to all sorts of problems

If you want to use integer values instead of floats you would have to create your own struct. I once created my Vector3i struct for that very same purpose.