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;
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
add a comment |
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
This is part ofUpdate
? 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
add a comment |
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
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
c# unity3d instantiation destroy gameobject
edited Mar 27 at 7:13
TeachMeEverything
asked Mar 27 at 5:23
TeachMeEverythingTeachMeEverything
4311 bronze badges
4311 bronze badges
This is part ofUpdate
? 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
add a comment |
This is part ofUpdate
? 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
add a comment |
2 Answers
2
active
oldest
votes
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);
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
add a comment |
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.
This code does not destroy the newGameObject.
– TeachMeEverything
Mar 27 at 6:41
add a comment |
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
);
);
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
Required, but never shown
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
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);
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
add a comment |
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);
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
add a comment |
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);
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);
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
add a comment |
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
add a comment |
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.
This code does not destroy the newGameObject.
– TeachMeEverything
Mar 27 at 6:41
add a comment |
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.
This code does not destroy the newGameObject.
– TeachMeEverything
Mar 27 at 6:41
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
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
Required, but never shown
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
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
Required, but never shown
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
Required, but never shown
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
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
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