No, this is bad
When you call Destroy()
in Unity, that object is gone. You can pass around a reference to that object all you want, but that reference points to a null object (sort of).
For the same frame during which the object is destroyed, the reference will continue to function normally (see test code examples), however, on the next update cycle those references will point to null as Unity has deconstructed the object and it no longer exists. Attempting to reference it will throw the runtime exception:
MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
The following examples were what I tested:
public class TestDestroy_NonCrashing : MonoBehaviour {
public GameObject someObj; //assigned in the inspector
void Start () {
Destroy(someObj);
Debug.Log(someObj); //prints "Cube (UnityEngine.GameObject)"
MeshRenderer someComponent = someObj.GetComponent<MeshRenderer>();
someComponent.material.SetColor("_Color", new Color(1, 0, 0));
}
}
public class TestDestroy_Crashing : MonoBehaviour {
public GameObject someObj; //assigned in the inspector
void Start () {
Destroy(someObj);
}
void Update() {
Debug.Log(someObj); //prints "null"
MeshRenderer someComponent = someObj.GetComponent<MeshRenderer>();
someComponent.material.SetColor("_Color", new Color(1, 0, 0));
}
}