Reference Equality Glitching - Resolved

I have a class, Coordinate, inheriting from Object. Coordinate instances are created at runtime. But there is an issue, which I have narrowed down to this:

//coord and upCoord are created elsewhere, I don't think that part really matters, so:
//Coordinate.z is a nonstatic integer variable

print((upCoord == coord) + ", " + (upCoord.z == coord.z));
//this prints out True, False

How is this possible, and - more importantly - what can I do? upCoord and coord should not refer to the same object.

Edit 1: For more context, the effect of the issue occurs in List Contains() method evaluating to true when it should evaluate to false.

Edit 2: Coordinate class (non-relevant code removed):

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

public class Coordinate : Object
{
    public readonly int x;
    public readonly int z;

    public enum TileType { abyss, grass, water };
    public TileType myType;

    public Coordinate(int x, int z)
    {
        this.x = x;
        this.z = z;
        myType = FindTileType();
    }

    public Coordinate(int x, int z, TileType type)
    {
        this.x = x;
        this.z = z;
        myType = type;
    }}

Start() and Update() are empty, other code functions and variables should have no bearing on this.

I think it is because UnityEngine.Object overloads the equality operator. This makes upCoord and coord each evaluate to true, and since true == true, then that’s true. That’s just my first guess.

Any particular reason you’re inheriting from such a thing? After eight years tinkering with Unity I’ve never seen the need…

You might have better luck using the .Equals() method but I’m not sure.

Can you share your code for the Coordinate class? Even Unity’s overridden == operator shouldn’t cause this.

I don’t remember for sure, but I believe that inheriting from Monobehavior prevents the instantiation of objects of that class. The Coordinate objects are never added as a component to any gameobject, they essentially just exist in lists for data storage and organization.

I can’t really use .Equals() because the issue is that .Contains() on a list should return false, but it returns true.

Let me rephrase my question:

Why are you inheriting from ANYTHING to make this coordinate class?

Why not just have a class:

public class MyCoordinate
{
  public int x;
  public int y;
}

If you want to edit it in the inspector window, mark it with System.SerializableAttribute

2 Likes

I just tried that; it froze on play. The class does use Phsyics.Raycast and Vector3, if it makes a difference.

That indicates a problem with your code. Most likely an infinite loop. Would you mind sharing your code for Coordinate here?

This is very likely. Are you testing for equality with a floating point number, such as what comes back in RaycastHit ? Never test for floating point equality, due to floating point error. Either downcast it to an integer, round it, or check if it is “close enough.”

Well, turns out that you were right. Making it not inherit from Object fixed it. It turns out that it was, as you said, an infinite loop, but in another class in the project. It triggered my infinite loop safety mechanism, it just took way longer to trigger than I thought it would. So, as far as I can tell, this issue is resolved. My bad, sorry…

AWESOME!

Not at all, my pleasure! Best luck moving forward with your project. Hit back if you have any more questions!