The Destroy method of the pit stepped on by Unity

When writing a function, I encountered a problem.

This function is to name the objects in order after the objects are destroyed, but the naming method is always executed before the Destroy method is executed, resulting in naming confusion.

I checked it out, and it turns out that the Destroy method is executed uniformly before the Update is executed ! !

When the Destroy method destroys an object, it does not destroy it immediately, but before the next Update execution (the Update methods of all objects are executed uniformly, and it will not happen that one object starts to execute Update while another object is still processing FixedUpdate. case)
· Between the previous item, after calling the Destroy method, you can continue to access the methods or other components in the script mounted by this object, but in the next frame, the object will be destroyed, and the reference to the object will also be Is empty.
·When the Destroy method is called, the OnDisable of the object will be called immediately (if the object is valid), but there is no strict order relationship between the two methods for the destruction of multiple objects (only related to the execution position, OnDestroy before the next frame).
————————————————
Copyright statement: This article is an original article of CSDN blogger "Ten Yuan" and follows the CC 4.0 BY-SA copyright agreement. For reprinting, please attach the original source link and this statement.
Original link: https://blog.csdn.net/ShihEuan/article/details/106202766

 Solution: Replace Destroy with DestroyImmediate.

The specific difference between the two can be seen in the Destroy and null problems in Unity_unity destroy does not exist_The Far Cloud Blog-CSDN Blog

Guess you like

Origin blog.csdn.net/makyocute/article/details/130240356