I was working on setting up IsValidActionGridPosition and when I would select my player and then click on a new location for him to move, Unity would completely crash. After a while I found that:
validGridPositions.Contains(gridPosition);
was the culprit. After I stepped through the code I found that the Equals method in GridPosition was causing the crash. At the time it looked like:
public bool Equals(GridPosition other)
{
return this.Equals(other);
}
When I would step through it, it seemed like the values wouldn’t change, it seemed strange. After running it over ten times and each time Unity would completely crash and quit. I finally changed it to this:
public bool Equals(GridPosition other)
{
return this.x == other.x && this.z == other.z;
}
This immediately worked and I was able to remove all of my troubleshooting debugs as well as my try and catch around the contains. I just looked at your source code and you had:
public bool Equals(GridPosition other)
{
return this == other;
}
Which would work because we defined the == method to be what I ended up trying at the end.
I’m going to leave this here in case someone was as stupid as I was and accidentally used “this.Equals(other)” instead of “this == other”.
It crashed because I was calling Equals from the Contains function and then accidentally being recursive calling Equals inside of the Equals method. So it would just keep calling itself until it crashed. That’s why it “looked” like the values wouldn’t change. It never would get past the first comparison.
Thanks Code Monkey!
Julian