HashCode.Combine(x, z); HashCode didn't exist!

I’ve installed the Microsoft.Bcl.HashCode via NuGet but still getting the error:

The name 'HashCode' does not exist in the current context GridPosition.cs

Also, using System;
:frowning:

Oh I see, it’s about .Net and Unity 2020.3 :-1:

Anyway at the moment is working even if I return 0, so I hope I can continue to follow the course :sweat_smile:

public override int GetHashCode()
        {
            return 0; // HashCode.Combine(x, z);
        }

Yup it is related to the .NET version so some older Unity versions won’t have it
If you can’t upgrade the version then implement it some other way instead of defaulting to 0, that might break some equality comparisons.
Here’s some quick code I found on a google search https://stackoverflow.com/a/371350

Returning 0 means that certain equality methods will consider all GridPositions to be equal…

Here’s a simple method that should work for now:

public override int GetHashCode()
{
    return x * 10000 + z;
}

It’s not pretty, but each GridPosition should yield a unique hashcode.

2 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms