How do I compare two objects to see if they're equal?

public static List<Monster> Monsters;

public static T GetClosestMonsterOfType<T>(GameObject CurObj) where T : Monster
		{
			return GetMonstersOfType<T>().Where(CurMonster => {
			//if((CurMonster as GameObject) != CurObj) 
			if(object.ReferenceEquals(CurMonster, CurObj)) 
			{
			Debug.Log("I am Not the CurObj! I am " + CurMonster.name + " While Cur Obj is " + CurObj.name);
				return true;
			}else{
				Debug.Log("I am him");
				return false;
			}
		}
		).OrderBy( monster => Vector3.Distance((monster as GameObject).transform.position, CurObj.transform.position)).First();
		}

I’ve tried both ReferenceEquals and casting the object to the same type just to see if it would work. Unfortunately no matter what I do, it always prints “I am Not the CurObj! etc.” Even with both names being the same. If I use ReferencEquals it always prints “I am him” no matter what the CurObj is.

All this code is supposed to do is to return the Monster with the lowest distance. Of course if the CurObj is in the Monsters list its distance to itself would be 0 which would make the method return itself. That’s why I’m trying to filter the list to only Objects that aren’t the CurObj before sorting it. If you need any more information please ask.

My guess (I don’t know what types some of your variables are or what functions return) but if CurMonster is Monster, and CurObj is GameObject, then they are pointing to two different things. CurMonster is pointing to a Monster class, and CurObj is pointing to a Game Object, they will never equal each other because they are separate instances of different things.

If Monster extends MonoBehaviour, you might want to check CurMonster.gameObject == CurObj.

I have no idea why I didn’t think of that instead of doing (CurMonster as GameObject). That worked, thanks!