- Home /
How to delete instantiated GameObject
I have a simple question , how can I delete a cloned or instantiated object after 1 second without deleting the original. The GameObject is just a 3d object with no scripts attached. All I want to do is instantiate an object at certain coordinates when required and destroy it after 1 second. Any help will be appreciated
If one of the below answers has resolved your concern, please vote up and accept the answer that best addresses your question. Any other questions that may have helped, an upvote would be nice as well.
DEAR @Srimasis: Is there anything else related to we could do for you? - If so, please tell us. Else please tick an answer if it helped you solve your problem to keep the board clean and tidy (it's a way of saying THAN$$anonymous$$ YOU for taking the time to answer and help me), if nothing helped you, then we really appreciate FEEDBAC$$anonymous$$.
Answer by clunk47 · Sep 10, 2013 at 07:53 PM
Here's a c# example. In this example, I define a GameObject where you will assign a prefab or original to in the inspector. Then we'll 'clone' the object, and instantiate a new instance of it, then destroy it after one second.
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour
{
public GameObject original;
void Awake()
{
GameObject clone = (GameObject)Instantiate (original, transform.position, Quaternion.identity);
Destroy (clone, 1.0f);
}
}
Here's how you could do the same as above, but in Javascript (Unityscript).
#pragma strict
var original : GameObject;
function Awake()
{
var clone = Instantiate (original, transform.position, Quaternion.identity);
Destroy (clone, 1.0);
}
Click here for more info on Destroy(object, float time).
Thanks brother but I have a doubt, what if I declare the variable clone as an array, I have to make multiple clones.
You don't just instantiate an array. Say you have a gameObject array with 10 gameObjects. You'd say something like:
var gameObjectArray : GameObject[];
var clone = Instantiate(gameObjectArray[3], transform.position, Quaternion.identity);
This is how you would for example, grab the 4th object from your array and create a new instance.
clunk47 used right code. It works. The GameObject clone means the instantiated clone of the original GameObject. And Destroy(clone, 1.0f) means that destroy the clone GameObject after 1 second. Thanks clunk47.
Answer by FrimaMickD · Sep 10, 2013 at 07:58 PM
GameObject mMyClone = (GameObject)GameObject.Instantiate(mOriginalObject); //Create the clone of mOriginalObject
GameObject.Destroy(mMyClone);//Destroy the Clone
GameObject.DestroyImmediate(mMyClone);//Destroy without waiting for GC (I think?)
Answer by getyour411 · Sep 10, 2013 at 07:55 PM
var myVar = Instantiate(go, position, rotation);
Destroy(myVar, 1);
If you "got it", please ACCEPT THE ANSWER that best addresses your question.
Answer by Bassem_Magdi · Jul 29, 2020 at 02:09 AM
You can simply nested it into a Destroy function and set the second argument with 1 "seconds" . Destroy( Instantiate(Dust, transform.position, transform.rotation), 1);
Your answer
Follow this Question
Related Questions
Destroy a specific instantiated clone? 2 Answers
How can I destroy my Instance without renaming it? 2 Answers
Why could I delete my clones onlY in the same order of instantiation? 2 Answers
restricting clone number 2 Answers
Deleting a GameObject 2 Answers