Search Unity

Comparing two different objects returns True

Discussion in 'Scripting' started by _6__, Apr 12, 2020.

  1. _6__

    _6__

    Joined:
    Jul 13, 2015
    Posts:
    11
    I have an A-Star script that used to work fine, but all of sudden has started doing something weird.

    One of the first checks the script does is to make sure that the current cell we're on and the cell we want to get to aren't the same cell. If they are it means we've successfully calculated a path.

    However, when I step over the code in Visual Studio, the comparison operator in the code below returns True, even though they have different values.



    Any idea what might be causing this to happen?

    The only thing I changed on this script was that I made the class Static, but this wouldn't be the issue would it?
     
  2. SINePrime

    SINePrime

    Joined:
    Jan 24, 2019
    Posts:
    54
    How is the == operator implemented for this type? It might not check if those values are equal.
     
    Kurt-Dekker likes this.
  3. _6__

    _6__

    Joined:
    Jul 13, 2015
    Posts:
    11
    You're right. I have no idea why, but it changed from comparing values to comparing type instead. I had to override Equals and ==, but also needed to use a 'currentNode is null' check rather than 'currentNode == null' in a few places.

    So things are working now, but its odd that == suddenly just changed what it was comparing.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,914
    It's a bit confusing, but when you use the "==" operator from within an == override method, you usually want to use https://docs.microsoft.com/en-us/dotnet/api/system.object.referenceequals?view=netframework-4.8 instead, otherwise you may be recursively calling your own == override instead of comparing object IDs.