Unit Data only updating if moving diagonally onto a Grid

Hi a strange one here.
If I move the selected unit either on an adjacent Grid in the X or Z axis the Unit data does not update. But if I move the selected Unit onto a diagonally adjacent Grid the Unit data updates as normal.

I have re-watched the video numerous times and to my mind the code is the same.

Has anyone had this issue?



Many Thanks

In the future, please don’t paste screenshots of your code here. It makes it really difficult to help. See this post on how to format code in the posts

In your GridPosition, the != operator is wrong. This will say that grid position a is != to grid position b only if both the X and Z values are different. if you say (10, 5) != (2, 5) it will return false, meaning they are the same. But (10, 5) == (2, 5) will also return false, meaning they’re not the same…

The way we usually do the override for != is to just negate ==

public static bool operator !=(GridPosition a, GridPosition b)
{
    return !(a == b);
}

If you really want to do the full code bit, you need to change the && to ||

public static bool operator !=(GridPosition a, GridPosition b)
{
    return a.x != b.x || a.z != b.z;
}

It’s for consistency’s sake that we negate ==. If one says yes, the other should say no. In your case both could say no - which is inconsistent: How can a equal b and not equal b at the same time?

1 Like

Thank you :slight_smile:

And yes of course will follow the correct way to post screenshots.

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

Privacy & Terms