Unity3d - Do I need to destroy gameobject AND script?

The name of the pictureThe name of the pictureThe name of the pictureClash 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);







share|improve this question
















  • 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
















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);







share|improve this question
















  • 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












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);







share|improve this question












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);









share|improve this question











share|improve this question




share|improve this question










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












  • 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










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.






share|improve this answer
















  • 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










Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "53"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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






























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.






share|improve this answer
















  • 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














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.






share|improve this answer
















  • 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












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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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












  • 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

















 

draft saved


draft discarded















































 


draft saved


draft discarded














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













































































Comments

Popular posts from this blog

What does second last employer means? [closed]

List of Gilmore Girls characters

Confectionery