Line 2 executing before line 1 - Is there a definitive guide to out of sequence functions?

I recently experienced issues Destroying objects whereby the reference to the old object was valid in script even though the object was destroyed and the reference replaced with a new object. I’ve now just experienced a null error from this code:

primary.SetActive(false);
primary=null;

Primary is null in the SetActive call even though I don’t set it to null until after.

So is there a guide/reference anywhere to which Unity actions can happen out of sequence or are otherwise buffered to execute at the end of frame? There seems no mention on the scripting reference. That is, Unity - Scripting API: GameObject.SetActive doesn’t say that the SetActive won’t happen until the next update, leading me to expect it to be immediate, in which case the code shouldn’t execute out of order as it is.

Where are you writing this? In the Awake() function?

Everything is immediate unless stated otherwise in the docs. Destroy does not remove the object until the end of the frame, but that’s the only exception I can think of offhand (and is stated clearly in the description). SetActive is immediate and the code you posted runs without errors, so primary was null when the code was run. You can check that with “if (primary != null)” before executing code that depends on primary not being null.

–Eric

Works as expected. Eric is probably right, it is mostly likely null already.

Thanks. I can only guess I created some muddled reference somewhere then as it’s not exhibiting the problem since whatever changes I’ve made!