Search Unity

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

Discussion in 'Scripting' started by SparrowsNest, Jan 8, 2019.

  1. SparrowsNest

    SparrowsNest

    Joined:
    Apr 6, 2017
    Posts:
    1,249
    I have this line in a mb that also implements someInterface
    Code (CSharp):
    1. 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
     
    Last edited: Jan 8, 2019
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    1,330
    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.
     
  3. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    2,882
    I guess you want to do this instead?
    Code (CSharp):
    1. if (this is someInterface)
    If you want to check that this implements someInterface.
     
    xVergilx likes this.
  4. SparrowsNest

    SparrowsNest

    Joined:
    Apr 6, 2017
    Posts:
    1,249
    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.