- Home /
Too subjective and argumentative
object can't be null when highlighted in the inspector?
Long story short: I have an inventory system that works fine, until I highlight one of the slots in the inspector during run-time, thus, exposing the 'item' object in the inspector. My code checks for, and denies, any interaction with null items in empty slots, but it seems that when I'm looking at an object in the inspector its not null anymore, its just blank. suddenly that slot that I'm looking at in the inspector can infinitely spawn 'blank' objects, even though its supposed to be null.... is this really whats happening? I even put a Debug.Log in the constructor and it runs when i first look at the item in the inspector during run-time.
side note: I'm not looking to paste my code in and have someone go over it. I really just want to know if there's some funny business going on when an object is highlighted in the inspector.
Actually you should post your code otherwise it just becomes a guessing game for everyone.
this question really has nothing to do with my code, at all. this question is completly based on the mechanics of unity itself. i just wanted to know why an object gets instantiated when it gets exposed in the inspector during run-time.
It really has got everything to do with your code. As far as I'm aware, nobody has ever reported Unity behaviour where highlighting an object in the inspector infinitely spawns "blank" objects. I'm not even sure what a blank object is. You're sure your code doesn't call Instantiate() at any point....?
Answer by hardmode2236 · Jun 13, 2016 at 02:02 AM
i'm just gonna go ahead and close this, i failed to explain my real question well enough and everyone is just gonna down-vote my ass into the dirt for trying to not involve my code, when all i wanted to know was if highlighting objects in the inspector could run some functions of the object for some reason.
It seems when my object was null, and i highlighted it in the inspector (in run-time) the constructor was called for some reason and the object became not null anymore. this is the root of my issue, and is the answer to my question.
The usual trick, when you have complicated/NDA'd/etc code, is to try to isolate the problem with a simplified example. (a serialized 1-item class, in an array (or not?)) If that acts funny, posting that code can often save a lot of typing.
But that sounds about right. Of course, Unity automatically new's anything visible in the Inspector. I'm a little surprised it happens while running, but I guess there's no real reason to click on a null item, anyway. So sort of a bug, but not a serious one.
Yup, happened to me too and drove me crazy for a while. I want to add that Unity doesn't necessarily call the constructor!
If, for example, the public variable (that is visible to the inspector) refers to a class that only has a constructor with parameters (no parameterless constructor) then Unity may allocate memory for you without calling any constructor. The public variable will refer to an instance that has not been initialized.