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.