Unity3d - Do I need to destroy gameobject AND script?
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
3
down vote
favorite
From proper leak protection do I need to delete both of these or will getting rid of one take care of both?
Currently I am destroying the script AND the gameobject
private void OnDestroy()
Destroy(this.gameObject);
Destroy(this);
unity gameobject memory-efficiency
add a comment |Â
up vote
3
down vote
favorite
From proper leak protection do I need to delete both of these or will getting rid of one take care of both?
Currently I am destroying the script AND the gameobject
private void OnDestroy()
Destroy(this.gameObject);
Destroy(this);
unity gameobject memory-efficiency
1
I don't see why you would need this in the first place .. OnDestroy is called anyway when the component already gets destroyed .. so why do you think you have to destroy it again?
– derHugo
Aug 18 at 4:56
The rest depends on what you need .. sometimes you only want to remove one component .. something you want to remove the entire GameObject..
– derHugo
Aug 18 at 5:06
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
From proper leak protection do I need to delete both of these or will getting rid of one take care of both?
Currently I am destroying the script AND the gameobject
private void OnDestroy()
Destroy(this.gameObject);
Destroy(this);
unity gameobject memory-efficiency
From proper leak protection do I need to delete both of these or will getting rid of one take care of both?
Currently I am destroying the script AND the gameobject
private void OnDestroy()
Destroy(this.gameObject);
Destroy(this);
unity gameobject memory-efficiency
asked Aug 17 at 14:04


Jacksonkr
1326
1326
1
I don't see why you would need this in the first place .. OnDestroy is called anyway when the component already gets destroyed .. so why do you think you have to destroy it again?
– derHugo
Aug 18 at 4:56
The rest depends on what you need .. sometimes you only want to remove one component .. something you want to remove the entire GameObject..
– derHugo
Aug 18 at 5:06
add a comment |Â
1
I don't see why you would need this in the first place .. OnDestroy is called anyway when the component already gets destroyed .. so why do you think you have to destroy it again?
– derHugo
Aug 18 at 4:56
The rest depends on what you need .. sometimes you only want to remove one component .. something you want to remove the entire GameObject..
– derHugo
Aug 18 at 5:06
1
1
I don't see why you would need this in the first place .. OnDestroy is called anyway when the component already gets destroyed .. so why do you think you have to destroy it again?
– derHugo
Aug 18 at 4:56
I don't see why you would need this in the first place .. OnDestroy is called anyway when the component already gets destroyed .. so why do you think you have to destroy it again?
– derHugo
Aug 18 at 4:56
The rest depends on what you need .. sometimes you only want to remove one component .. something you want to remove the entire GameObject..
– derHugo
Aug 18 at 5:06
The rest depends on what you need .. sometimes you only want to remove one component .. something you want to remove the entire GameObject..
– derHugo
Aug 18 at 5:06
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
12
down vote
accepted
You only need to destroy the GameObject.
By destroying the GameObject (Destroy(this.gameObject);
), you also destroy the script (Destroy(this)
) automatically.
Destroying the script simply removes the component from the GameObject. Destroying the GameObject removes all of its components, and the GameObject itself.
But there is an issue with your code. OnDestroy()
is called when the component is being destroyed. (This could be because the component is destroyed, or its GameObject.) So calling the code in there is probably not what you want.
4
This is completely intuitive when seeing that GameObjects don't really do anything and are just "bags of components" with a name and ID. Everything that a GameObject "does" is actually a component doing something.
– R. Schmitz
Aug 17 at 15:27
It totally depends on the situation ... Sometimes removing only the component is exactly what you want to do.. sometimes you want to destroy the entire object. Your last section is the actual answer for the OP: destroying it again in OnDestroy makes no sense ....
– derHugo
Aug 18 at 5:08
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
12
down vote
accepted
You only need to destroy the GameObject.
By destroying the GameObject (Destroy(this.gameObject);
), you also destroy the script (Destroy(this)
) automatically.
Destroying the script simply removes the component from the GameObject. Destroying the GameObject removes all of its components, and the GameObject itself.
But there is an issue with your code. OnDestroy()
is called when the component is being destroyed. (This could be because the component is destroyed, or its GameObject.) So calling the code in there is probably not what you want.
4
This is completely intuitive when seeing that GameObjects don't really do anything and are just "bags of components" with a name and ID. Everything that a GameObject "does" is actually a component doing something.
– R. Schmitz
Aug 17 at 15:27
It totally depends on the situation ... Sometimes removing only the component is exactly what you want to do.. sometimes you want to destroy the entire object. Your last section is the actual answer for the OP: destroying it again in OnDestroy makes no sense ....
– derHugo
Aug 18 at 5:08
add a comment |Â
up vote
12
down vote
accepted
You only need to destroy the GameObject.
By destroying the GameObject (Destroy(this.gameObject);
), you also destroy the script (Destroy(this)
) automatically.
Destroying the script simply removes the component from the GameObject. Destroying the GameObject removes all of its components, and the GameObject itself.
But there is an issue with your code. OnDestroy()
is called when the component is being destroyed. (This could be because the component is destroyed, or its GameObject.) So calling the code in there is probably not what you want.
4
This is completely intuitive when seeing that GameObjects don't really do anything and are just "bags of components" with a name and ID. Everything that a GameObject "does" is actually a component doing something.
– R. Schmitz
Aug 17 at 15:27
It totally depends on the situation ... Sometimes removing only the component is exactly what you want to do.. sometimes you want to destroy the entire object. Your last section is the actual answer for the OP: destroying it again in OnDestroy makes no sense ....
– derHugo
Aug 18 at 5:08
add a comment |Â
up vote
12
down vote
accepted
up vote
12
down vote
accepted
You only need to destroy the GameObject.
By destroying the GameObject (Destroy(this.gameObject);
), you also destroy the script (Destroy(this)
) automatically.
Destroying the script simply removes the component from the GameObject. Destroying the GameObject removes all of its components, and the GameObject itself.
But there is an issue with your code. OnDestroy()
is called when the component is being destroyed. (This could be because the component is destroyed, or its GameObject.) So calling the code in there is probably not what you want.
You only need to destroy the GameObject.
By destroying the GameObject (Destroy(this.gameObject);
), you also destroy the script (Destroy(this)
) automatically.
Destroying the script simply removes the component from the GameObject. Destroying the GameObject removes all of its components, and the GameObject itself.
But there is an issue with your code. OnDestroy()
is called when the component is being destroyed. (This could be because the component is destroyed, or its GameObject.) So calling the code in there is probably not what you want.
answered Aug 17 at 14:05


Evorlor
2,25831861
2,25831861
4
This is completely intuitive when seeing that GameObjects don't really do anything and are just "bags of components" with a name and ID. Everything that a GameObject "does" is actually a component doing something.
– R. Schmitz
Aug 17 at 15:27
It totally depends on the situation ... Sometimes removing only the component is exactly what you want to do.. sometimes you want to destroy the entire object. Your last section is the actual answer for the OP: destroying it again in OnDestroy makes no sense ....
– derHugo
Aug 18 at 5:08
add a comment |Â
4
This is completely intuitive when seeing that GameObjects don't really do anything and are just "bags of components" with a name and ID. Everything that a GameObject "does" is actually a component doing something.
– R. Schmitz
Aug 17 at 15:27
It totally depends on the situation ... Sometimes removing only the component is exactly what you want to do.. sometimes you want to destroy the entire object. Your last section is the actual answer for the OP: destroying it again in OnDestroy makes no sense ....
– derHugo
Aug 18 at 5:08
4
4
This is completely intuitive when seeing that GameObjects don't really do anything and are just "bags of components" with a name and ID. Everything that a GameObject "does" is actually a component doing something.
– R. Schmitz
Aug 17 at 15:27
This is completely intuitive when seeing that GameObjects don't really do anything and are just "bags of components" with a name and ID. Everything that a GameObject "does" is actually a component doing something.
– R. Schmitz
Aug 17 at 15:27
It totally depends on the situation ... Sometimes removing only the component is exactly what you want to do.. sometimes you want to destroy the entire object. Your last section is the actual answer for the OP: destroying it again in OnDestroy makes no sense ....
– derHugo
Aug 18 at 5:08
It totally depends on the situation ... Sometimes removing only the component is exactly what you want to do.. sometimes you want to destroy the entire object. Your last section is the actual answer for the OP: destroying it again in OnDestroy makes no sense ....
– derHugo
Aug 18 at 5:08
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgamedev.stackexchange.com%2fquestions%2f162892%2funity3d-do-i-need-to-destroy-gameobject-and-script%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
I don't see why you would need this in the first place .. OnDestroy is called anyway when the component already gets destroyed .. so why do you think you have to destroy it again?
– derHugo
Aug 18 at 4:56
The rest depends on what you need .. sometimes you only want to remove one component .. something you want to remove the entire GameObject..
– derHugo
Aug 18 at 5:06