How do I destroy an instantiated game object clone?Deep cloning objectsHow do I enumerate an enum in C#?unity Destroy instantiated GameObjectDestroying Clone in UnityUnable to delete clones made with instantiation functionWhy all the cloned objects are not childs under gameobject Sphere?Prefab game object isn't being Destroyedwhen instantiated gameobject is destroyed it does not produce anymore clonesHow to keep checking if a gameobject will be instantiated in hierarchy in unity?How to destroy GameObject & Instantiate new one after a while

Determine Beckett Grading Service (BGS) Final Grade

Is it true that control+alt+delete only became a thing because IBM would not build Bill Gates a computer with a task manager button?

Probably terminated or laid off soon; confront or not?

How to draw a flow chart?

What could prevent players from leaving an island?

Did Captain America make out with his niece?

How to halve redstone signal strength?

Is this cheap "air conditioner" able to cool a room?

Based on what criteria do you add/not add icons to labels within a toolbar?

Should I self-publish my novella on Amazon or try my luck getting publishers?

Will a paper be retracted if a flaw in released software code invalidates its central idea?

ESTA declined to the US

How can glass marbles naturally occur in a desert?

Why do private jets such as Gulfstream fly higher than other civilian jets?

Pronouns when writing from the point of view of a robot

Can you use the Help action to give a 2019 UA Artillerist artificer's turret advantage?

Getting an entry level IT position later in life

Did Apollo leave poop on the moon?

Can I enter a rental property without giving notice if I'm afraid a tenant may be hurt?

Is it a bad idea to offer variants of a final exam based on the type of allowed calculators?

Is the Folding Boat truly seaworthy?

How to check a file was encrypted (really & correctly)

Why don't the open notes matter in guitar chords?

Why do proponents of guns oppose gun competency tests?



How do I destroy an instantiated game object clone?


Deep cloning objectsHow do I enumerate an enum in C#?unity Destroy instantiated GameObjectDestroying Clone in UnityUnable to delete clones made with instantiation functionWhy all the cloned objects are not childs under gameobject Sphere?Prefab game object isn't being Destroyedwhen instantiated gameobject is destroyed it does not produce anymore clonesHow to keep checking if a gameobject will be instantiated in hierarchy in unity?How to destroy GameObject & Instantiate new one after a while






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I am trying to check if an object appears above a number. And if it does I want to destroy it and recreate it. And if it does not I want to create it. I have tried the following code. It creates the object but does not destroy it.



GameObject newGameObject = null;
if (x > 3.14)

if (newGameObject != null && newGameObject.scene.IsValid())

Destroy(newGameObject);
newGameObject = GameObject.Instantiate(object1);

else

newGameObject = GameObject.Instantiate(object1);



else

Destroy(newGameObject);




When running multiple times the objects never destroy but stack and I can see them in the hierarchy not being destroyed.



I have tried giving the newGameObject a tag so that I could destroy the tagged item and it did not tag any clones.



I tried find name of gameobject + "(clone)" and destroy them but it did not let me.



I have tried adding 1f timer, destroy(newGameObject,1f) does not destroy game object.










share|improve this question


























  • This is part of Update? It is weird as you declare object multiple times...

    – Johnny
    Mar 27 at 5:34











  • It is part of onclick, but it could be part of update I assume it could be used in update to check values of an updating source.

    – TeachMeEverything
    Mar 27 at 5:37











  • It was a typo that I wrote GameObject

    – TeachMeEverything
    Mar 27 at 6:02

















1















I am trying to check if an object appears above a number. And if it does I want to destroy it and recreate it. And if it does not I want to create it. I have tried the following code. It creates the object but does not destroy it.



GameObject newGameObject = null;
if (x > 3.14)

if (newGameObject != null && newGameObject.scene.IsValid())

Destroy(newGameObject);
newGameObject = GameObject.Instantiate(object1);

else

newGameObject = GameObject.Instantiate(object1);



else

Destroy(newGameObject);




When running multiple times the objects never destroy but stack and I can see them in the hierarchy not being destroyed.



I have tried giving the newGameObject a tag so that I could destroy the tagged item and it did not tag any clones.



I tried find name of gameobject + "(clone)" and destroy them but it did not let me.



I have tried adding 1f timer, destroy(newGameObject,1f) does not destroy game object.










share|improve this question


























  • This is part of Update? It is weird as you declare object multiple times...

    – Johnny
    Mar 27 at 5:34











  • It is part of onclick, but it could be part of update I assume it could be used in update to check values of an updating source.

    – TeachMeEverything
    Mar 27 at 5:37











  • It was a typo that I wrote GameObject

    – TeachMeEverything
    Mar 27 at 6:02













1












1








1








I am trying to check if an object appears above a number. And if it does I want to destroy it and recreate it. And if it does not I want to create it. I have tried the following code. It creates the object but does not destroy it.



GameObject newGameObject = null;
if (x > 3.14)

if (newGameObject != null && newGameObject.scene.IsValid())

Destroy(newGameObject);
newGameObject = GameObject.Instantiate(object1);

else

newGameObject = GameObject.Instantiate(object1);



else

Destroy(newGameObject);




When running multiple times the objects never destroy but stack and I can see them in the hierarchy not being destroyed.



I have tried giving the newGameObject a tag so that I could destroy the tagged item and it did not tag any clones.



I tried find name of gameobject + "(clone)" and destroy them but it did not let me.



I have tried adding 1f timer, destroy(newGameObject,1f) does not destroy game object.










share|improve this question
















I am trying to check if an object appears above a number. And if it does I want to destroy it and recreate it. And if it does not I want to create it. I have tried the following code. It creates the object but does not destroy it.



GameObject newGameObject = null;
if (x > 3.14)

if (newGameObject != null && newGameObject.scene.IsValid())

Destroy(newGameObject);
newGameObject = GameObject.Instantiate(object1);

else

newGameObject = GameObject.Instantiate(object1);



else

Destroy(newGameObject);




When running multiple times the objects never destroy but stack and I can see them in the hierarchy not being destroyed.



I have tried giving the newGameObject a tag so that I could destroy the tagged item and it did not tag any clones.



I tried find name of gameobject + "(clone)" and destroy them but it did not let me.



I have tried adding 1f timer, destroy(newGameObject,1f) does not destroy game object.







c# unity3d instantiation destroy gameobject






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 27 at 7:13







TeachMeEverything

















asked Mar 27 at 5:23









TeachMeEverythingTeachMeEverything

4311 bronze badges




4311 bronze badges















  • This is part of Update? It is weird as you declare object multiple times...

    – Johnny
    Mar 27 at 5:34











  • It is part of onclick, but it could be part of update I assume it could be used in update to check values of an updating source.

    – TeachMeEverything
    Mar 27 at 5:37











  • It was a typo that I wrote GameObject

    – TeachMeEverything
    Mar 27 at 6:02

















  • This is part of Update? It is weird as you declare object multiple times...

    – Johnny
    Mar 27 at 5:34











  • It is part of onclick, but it could be part of update I assume it could be used in update to check values of an updating source.

    – TeachMeEverything
    Mar 27 at 5:37











  • It was a typo that I wrote GameObject

    – TeachMeEverything
    Mar 27 at 6:02
















This is part of Update? It is weird as you declare object multiple times...

– Johnny
Mar 27 at 5:34





This is part of Update? It is weird as you declare object multiple times...

– Johnny
Mar 27 at 5:34













It is part of onclick, but it could be part of update I assume it could be used in update to check values of an updating source.

– TeachMeEverything
Mar 27 at 5:37





It is part of onclick, but it could be part of update I assume it could be used in update to check values of an updating source.

– TeachMeEverything
Mar 27 at 5:37













It was a typo that I wrote GameObject

– TeachMeEverything
Mar 27 at 6:02





It was a typo that I wrote GameObject

– TeachMeEverything
Mar 27 at 6:02












2 Answers
2






active

oldest

votes


















2














If the code is all part of an OnClick callback then here is why you are not destroying any object:



The main culprit is your definition of GameObject newGameObject = null inside the method. The value of newGameObject will always be null inside of the method so the statement if (newGameObject != null && newGameObject.scene.IsValid()) will always return false because newGameObject is always null and that is why you see all those GameObjects being instantiated. The case when if(x > 3.14) fails does not destroy anything as you are trying to destroy newGameObject, which is null.



To be able to destroy the object you will need a class field to store the reference to the last GameObject spawned.



Your MonoBehaviour will then look something like this:



public class MyBehaviour : MonoBehaviour

private GameObject newGameObject;

/* other useful code */

public void OnClick()
if (x > 3.14)

if (newGameObject != null && newGameObject.scene.IsValid())

Destroy(newGameObject);
newGameObject = GameObject.Instantiate(object1);

else

newGameObject = GameObject.Instantiate(object1);


else

Destroy(newGameObject);








share|improve this answer

























  • Thank you very much. Removing GameObject newGameObject = null; and declaring it as a private fixed it, It now works perfectly.

    – TeachMeEverything
    Mar 27 at 7:52


















1














GameObject newGameObject = null;
if (x > 3.14)

if (newGameObject != null && newGameObject.scene.IsValid())

Destroy(newGameObject);
newGameObject = GameObject.Instantiate(object1);

else

newGameObject = GameObject.Instantiate(object1);



else

Destroy(newGameObject);



Try this. Everytime when you create a new object after destroy will create one new object.






share|improve this answer

























  • This code does not destroy the newGameObject.

    – TeachMeEverything
    Mar 27 at 6:41













Your Answer






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: "1"
;
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',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55370313%2fhow-do-i-destroy-an-instantiated-game-object-clone%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














If the code is all part of an OnClick callback then here is why you are not destroying any object:



The main culprit is your definition of GameObject newGameObject = null inside the method. The value of newGameObject will always be null inside of the method so the statement if (newGameObject != null && newGameObject.scene.IsValid()) will always return false because newGameObject is always null and that is why you see all those GameObjects being instantiated. The case when if(x > 3.14) fails does not destroy anything as you are trying to destroy newGameObject, which is null.



To be able to destroy the object you will need a class field to store the reference to the last GameObject spawned.



Your MonoBehaviour will then look something like this:



public class MyBehaviour : MonoBehaviour

private GameObject newGameObject;

/* other useful code */

public void OnClick()
if (x > 3.14)

if (newGameObject != null && newGameObject.scene.IsValid())

Destroy(newGameObject);
newGameObject = GameObject.Instantiate(object1);

else

newGameObject = GameObject.Instantiate(object1);


else

Destroy(newGameObject);








share|improve this answer

























  • Thank you very much. Removing GameObject newGameObject = null; and declaring it as a private fixed it, It now works perfectly.

    – TeachMeEverything
    Mar 27 at 7:52















2














If the code is all part of an OnClick callback then here is why you are not destroying any object:



The main culprit is your definition of GameObject newGameObject = null inside the method. The value of newGameObject will always be null inside of the method so the statement if (newGameObject != null && newGameObject.scene.IsValid()) will always return false because newGameObject is always null and that is why you see all those GameObjects being instantiated. The case when if(x > 3.14) fails does not destroy anything as you are trying to destroy newGameObject, which is null.



To be able to destroy the object you will need a class field to store the reference to the last GameObject spawned.



Your MonoBehaviour will then look something like this:



public class MyBehaviour : MonoBehaviour

private GameObject newGameObject;

/* other useful code */

public void OnClick()
if (x > 3.14)

if (newGameObject != null && newGameObject.scene.IsValid())

Destroy(newGameObject);
newGameObject = GameObject.Instantiate(object1);

else

newGameObject = GameObject.Instantiate(object1);


else

Destroy(newGameObject);








share|improve this answer

























  • Thank you very much. Removing GameObject newGameObject = null; and declaring it as a private fixed it, It now works perfectly.

    – TeachMeEverything
    Mar 27 at 7:52













2












2








2







If the code is all part of an OnClick callback then here is why you are not destroying any object:



The main culprit is your definition of GameObject newGameObject = null inside the method. The value of newGameObject will always be null inside of the method so the statement if (newGameObject != null && newGameObject.scene.IsValid()) will always return false because newGameObject is always null and that is why you see all those GameObjects being instantiated. The case when if(x > 3.14) fails does not destroy anything as you are trying to destroy newGameObject, which is null.



To be able to destroy the object you will need a class field to store the reference to the last GameObject spawned.



Your MonoBehaviour will then look something like this:



public class MyBehaviour : MonoBehaviour

private GameObject newGameObject;

/* other useful code */

public void OnClick()
if (x > 3.14)

if (newGameObject != null && newGameObject.scene.IsValid())

Destroy(newGameObject);
newGameObject = GameObject.Instantiate(object1);

else

newGameObject = GameObject.Instantiate(object1);


else

Destroy(newGameObject);








share|improve this answer













If the code is all part of an OnClick callback then here is why you are not destroying any object:



The main culprit is your definition of GameObject newGameObject = null inside the method. The value of newGameObject will always be null inside of the method so the statement if (newGameObject != null && newGameObject.scene.IsValid()) will always return false because newGameObject is always null and that is why you see all those GameObjects being instantiated. The case when if(x > 3.14) fails does not destroy anything as you are trying to destroy newGameObject, which is null.



To be able to destroy the object you will need a class field to store the reference to the last GameObject spawned.



Your MonoBehaviour will then look something like this:



public class MyBehaviour : MonoBehaviour

private GameObject newGameObject;

/* other useful code */

public void OnClick()
if (x > 3.14)

if (newGameObject != null && newGameObject.scene.IsValid())

Destroy(newGameObject);
newGameObject = GameObject.Instantiate(object1);

else

newGameObject = GameObject.Instantiate(object1);


else

Destroy(newGameObject);









share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 27 at 7:21









SaeleasSaeleas

4487 bronze badges




4487 bronze badges















  • Thank you very much. Removing GameObject newGameObject = null; and declaring it as a private fixed it, It now works perfectly.

    – TeachMeEverything
    Mar 27 at 7:52

















  • Thank you very much. Removing GameObject newGameObject = null; and declaring it as a private fixed it, It now works perfectly.

    – TeachMeEverything
    Mar 27 at 7:52
















Thank you very much. Removing GameObject newGameObject = null; and declaring it as a private fixed it, It now works perfectly.

– TeachMeEverything
Mar 27 at 7:52





Thank you very much. Removing GameObject newGameObject = null; and declaring it as a private fixed it, It now works perfectly.

– TeachMeEverything
Mar 27 at 7:52













1














GameObject newGameObject = null;
if (x > 3.14)

if (newGameObject != null && newGameObject.scene.IsValid())

Destroy(newGameObject);
newGameObject = GameObject.Instantiate(object1);

else

newGameObject = GameObject.Instantiate(object1);



else

Destroy(newGameObject);



Try this. Everytime when you create a new object after destroy will create one new object.






share|improve this answer

























  • This code does not destroy the newGameObject.

    – TeachMeEverything
    Mar 27 at 6:41















1














GameObject newGameObject = null;
if (x > 3.14)

if (newGameObject != null && newGameObject.scene.IsValid())

Destroy(newGameObject);
newGameObject = GameObject.Instantiate(object1);

else

newGameObject = GameObject.Instantiate(object1);



else

Destroy(newGameObject);



Try this. Everytime when you create a new object after destroy will create one new object.






share|improve this answer

























  • This code does not destroy the newGameObject.

    – TeachMeEverything
    Mar 27 at 6:41













1












1








1







GameObject newGameObject = null;
if (x > 3.14)

if (newGameObject != null && newGameObject.scene.IsValid())

Destroy(newGameObject);
newGameObject = GameObject.Instantiate(object1);

else

newGameObject = GameObject.Instantiate(object1);



else

Destroy(newGameObject);



Try this. Everytime when you create a new object after destroy will create one new object.






share|improve this answer













GameObject newGameObject = null;
if (x > 3.14)

if (newGameObject != null && newGameObject.scene.IsValid())

Destroy(newGameObject);
newGameObject = GameObject.Instantiate(object1);

else

newGameObject = GameObject.Instantiate(object1);



else

Destroy(newGameObject);



Try this. Everytime when you create a new object after destroy will create one new object.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 27 at 5:58









SivaSiva

3003 silver badges14 bronze badges




3003 silver badges14 bronze badges















  • This code does not destroy the newGameObject.

    – TeachMeEverything
    Mar 27 at 6:41

















  • This code does not destroy the newGameObject.

    – TeachMeEverything
    Mar 27 at 6:41
















This code does not destroy the newGameObject.

– TeachMeEverything
Mar 27 at 6:41





This code does not destroy the newGameObject.

– TeachMeEverything
Mar 27 at 6:41

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55370313%2fhow-do-i-destroy-an-instantiated-game-object-clone%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript