Destroy script and component it depends on

I have a custom script component that creates child tiled SpriteRenderers, then removes itself.

As implemented, it configures the new SpriteRenderers based on a SpriteRenderer attached to its GameObject, then Destroy()s itself and the source SpriteRenderer.

However, because I’ve put [RequireComponent(typeof(SpriteRenderer))] on the script, I have to Destroy() the script itself before Unity will allow me to Destroy() the SpriteRenderer that I no longer want.

So, my question is:

Given a script that performs the following actions, can I be sure that the current step will be finished before the script is Destroy()ed? In other words, can I put code after Destroy(this) and still count on it to run?

T component = this.GetComponent<T>();
// some logic
Destroy(this);
Destroy(component);

As scripting API states, “actual object destruction is always delayed until after the current Update loop, but will always be done before rendering”.

Also, I tested your code and it works as intended.

So yes, you can be sure at it.

i know to det rid of a object like a character you would have to do

void OnCollisionEnter(Collision Col){

if(col.gameObject.tag == “Player”){
destroy(col.gameObject);
}
}