- Home /
Destroy script and component it depends on
I have a custom script component that creates child tiled SpriteRenderer
s, then removes itself.
As implemented, it configures the new SpriteRenderer
s 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);
@CAD97 what do you want this script to do, to destroy the object its attached to or destroy a different object?
You could do it with a separate script I guess, or maybe there's some way of structuring that removes both? I'd have to look into it further.
@rooster2 it's destroying the script itself and a component on the GameObject it is attached to.
Answer by Supert · Apr 22, 2016 at 10:24 AM
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.
Answer by rooster2 · Apr 22, 2016 at 10:27 AM
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); } }
Your answer
Follow this Question
Related Questions
Unity Deleted My Game 1 Answer
i have some people walk around the scene but it will destroy after going away from the scene 1 Answer
How can I destroy GameObjects on the screen with the same name? 3 Answers
I have a bullet object and Enemy object i want to destroyed it when bullet hits enemy 1 Answer