- Home /
If statement executing even though argument is false
I have a function to check if an inventory slot is available, but the if statement has begun executing even if its argument is false. This only started happening after I changed some stuff before this function is called from another class, but nothing witthin this class itself. Am I missing something obvious or is my project cursed?
Are you sure both objects are null? Better check with debugger. $$anonymous$$ight be that they are not a nullable type (for example Vector).
Oh completely misread. Try hovering over the item and parentSlot values in the if statement. Sometimes VS doesnt update watches.
Answer by Bunny83 · Apr 06, 2020 at 02:43 AM
Well we don't know much about what item and parentSlot actually are. I never really tested this in the debugger but it might be possible that the watches VS is evaluating doesn't use Unity's overloaded == operator. So if your item and parentSlot references are of a type that is derived from UnityEngine.Object, technically they might not be null but both point to objects which have been destroyed / wrongly created. The overloaded == operator would fake that the objects are null in that case. This could be a bug in VS
If you have never heard about the custom == operator I highly recommend to read this blog post. Even if you already know about the == operator but haven't read the blog post yet, I still recommend reading it. It's a fundamental part of Unity to understand the two sides and how they interact with each other.
Note that this is just a guess based on what we can see in your question.