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
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).
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?)
var myVar = Instantiate(go, position, rotation);
Destroy(myVar, 1);
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);