Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We've updated our Terms of Service. Please read our blog post from Unity CTO and Co-Founder Joachim Ante here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

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:
    990
    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,101
    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,682
    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:
    990
    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.