Best Practice to destroy gameobject

I have experienced so many times that some of the script components are still trying to access the gameobject when the gameobject is already destroyed somewhere else.

It usually happens in this senario:

I Destroyed the gameobject A in the collision event B. But some script components of A are still running even after I destroyed A in the collision event of B because some scripts have async functions or some event-based function are called. Then I will get the MissingReferenceException error: The object of type ‘XXX’ has been destroyed but you are still trying to access it.

What I usually do is adding a flag isDestroy then I will change this flag first before destroying. Then add a condition check in the async function and event function.

That works usually. But it is annoying sometimes. I am wondering what is the best way to solve this problem. Since I can’t find any way to check the gameobject itself is null or not in its own script component.

I’m not really understand your question, but i think you just simplify check if null of not.

if(yourScript == null){//your script was destroyed.

}