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.
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.
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…