Possible unintended reference comparison. Consider casting the X side expression to type Y

I have this line in a mb that also implements someInterface

if(someInteface != this)

now, if I try to cast it i get an error when ever the reference to someInterface is from a different mb that implements it, the reason i didn’t do this in the first place.

why is the unity console bugging me about it?
Am i missing something? that line have been there for months without an issue, i’m getting pretty sick of seeing this warning pop up whenever i recompile the code

I think it’s because it is never “this” due to being interface. You need to cast it first to the type. Although, that might not work as well.

Problem with != is the same as == for the interface. It is overriden by Unity’s operator, but for the interfaces used == as reference comparison. So, you’re comparing references only, which causes that warning.

1 Like

I guess you want to do this instead?

if (this is someInterface)

If you want to check that this implements someInterface.

1 Like

I see. (sorry for the delay, I don’t remember being alerted about this)

well, the code I have actually runs perfectly fine so I don’t really feel like changing it, and I was actually wondering if there’s a way to tell unity to stop showing a warning in general.

Ctrl+. => Suppress CS0252

This warning is coming from C#. This comparsion will return true if someInterface refereces this, but if there’s any comparsion operation defined on that classes, it won’t be called. This warning warns you about this.