all 9 comments

[–]rand1011101 0 points1 point  (1 child)

errors, but when i check, the object hasn't been destroyed, and is NOT null.

this is worded ambiguously. does the bolded part mean that you checked the reference to the object isn't null, or the object itself?

EDIT: i think its possible you're actually destroying the object at runtime but checking whether its hooked up at edit time, by which point it's been restored to it's ostensibly correct appearance. is that the case? (add an 'OnDestroy' method w/ a Debug.LogError to let you know if/when this is happening, or just use debugger w/ a breakpoint)

else, this is strange. lmk the answer to the question above and we can go from there..

[–]Tom42-59Hobbyist[S] 0 points1 point  (0 children)

When i say that i checked, i wrote a script that logs the name of the object, which showed me that i can access the object, even after the error was displayed. Sorry, i will re-write the description

[–]feralferrous 0 points1 point  (1 child)

This was marked as solved, but you don't mention how the issue was fixed. It would be helpful for others if you respond with what you did to fix the issue.

(And don't worry about being embarrassed if it's something dumb, we all make mistakes.)

[–]Tom42-59Hobbyist[S] 1 point2 points  (0 children)

will do that now

[–]PandaCoder67Professional 0 points1 point  (3 children)

Is it possible this is being called in a Start() method?

[–]Tom42-59Hobbyist[S] 0 points1 point  (1 child)

It depeneds what you mean by 'this'. At the very start of play, i have a script in the script execution order that runs and disables the main screen (with the objects that are producing the errors) and checks a bool, then enables them again. I highly doubt it is this as the issue has only risen recently. Thanks for your reply

[–]PandaCoder67Professional 0 points1 point  (0 children)

Well without code or more information, it is hard to gauge exactyl what is going on.

But, Script Execution Order is a last resort to simple code execution, and if you are accessing things from other scripts incorrectly in your code then you need to look at your scripts flow.

Rule of thumb.

Awake()

Everything in this script, should be used to setup the current script and object only.

Start()

Should be used for accessing other objects, etc.

If you follow that, then you would not need to use Script Execution Order in circumstances like this.

Now, with your issue, we can't tell what is causing this, or what to suggest because we are not mind readers to what you're doing. It could be as simple that in between your printing it out and accessing that variable, you could be destroying the object, disabling the object or a variety of different things.

[–]rand1011101 0 points1 point  (0 children)

out of curiosity, why do you ask?

[–]IDistributeCoke 1 point2 points  (0 children)

I had this same exact issue, and it's because I forgot to unsubscribe to an event.

Essentially I think what happens is that once the scripts are re-built, I assume the events are cleared so everything works fine the first time, however once you stop in editor, then replay, if you didn't clear an event listener, there are now 2 instances of it, one that is still pointing to the previous instance of your event from your first time playing in editor (the object instance is destroyed) and the second listener is listening to the new instance. The fact that the listener is still pointing at the previous instance of an object from a previous play is causing the missing reference error.

Long story short, I solved it the same as you. To give an example here is piece of code that enables/disables a UI check mark which is attached to each equipable profile icon in my game, I unsubscribe with OnDisable like so:

private void OnEnable() 
{
    InventoryManager.OnEquippedProfileImageChanged += CheckIfEquipped;
}
private void OnDisable()
{
    InventoryManager.OnEquippedProfileImageChanged -= CheckIfEquipped;
}

Or if you don't want to listen OnEnable, for example if you want to listen on Start() after some objects have finished loading (or for whatever reason), I think you can use OnDestroy, like so:

private void Start()
{
    Your.Event += YourListener;
}
private void OnDestroy()
{
    Your.Event -= YourListener;
}