Question by
redgroupclan · May 30, 2016 at 02:43 PM ·
instantiatedelete
How do I delete an instance created by Instantiate?
Using Javascript. I have a capsule that spawns by Instantiate when the player presses Tab. How would I delete that capsule when the player presses Tab again?
Comment
Best Answer
Answer by ownerfate · May 30, 2016 at 06:49 PM
that would be done as thus: // had to do a little update, seeing this might destroy it as soon as tab is pressed.
2 ways ( or more ) you can handle this.
// you may have to bare with the tagging system for this method.
//javaScript // Tagging method.
function Update () {
if(Input.GetKeyDown(KeyCode.Tab)){
Destroy(GameObject.FindWithTag("place tag here"));// any type of tag works.
}
}
when tagging make sure the instanced object is tagged the same as the one you type in the code.
2: Placement
// this would be placed on the prefabbed object that is being destroyed the second time you press tab.
// all this does is just destroys it when it's in the game, when you press the tab key. it's similar to the first script but doesn't use tags.
function Update (){
if(Input.GetKeyDown(KeyCode.Tab)){
Destroy(this.gameObject);
}
}
hope this helps.
Your answer
![](https://koobas.hobune.stream/wayback/20220612081857im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to delete an instantiated GameObject 2 Answers
Using constructors in Unity (C#) 8 Answers
Performance of instantiate and destroy for bullets 2 Answers
Instantiate objects around a sphere 2 Answers
More problems with arrow shooting 0 Answers