delay a bullet clone using find tag

Hi there guys I am firing a instantiated bullet which I wish to destroy after a second.

I am using a find with tag to destroy the bullet but it destroys it immediately…and as I stated I wish it to destroy after a delay

function Update ()
if (Input.GetKey ("space"))	

var emellybullet = GameObject.FindWithTag("bullet");
Destroy (GameObject.Find ("bullet(Clone)"));

}

Why don’t attach a script to the bullet prefab, so as to check its time to live and just destroy it after 1 second?

Hi thanks for the reply however that just throws up a error rigidbody destroyed but you are still trying to access it im pretty certain the cloned object has to be referenced its the how is the problem…

Hey there, I’m rather new to Unity but I am doing something similar with the lasers that ships in my game fire. Each laser is a little prefab collider sphere (gravity turned off in the rigidbody) with a point light attached, and each prefab has the following code:

var lifeTime = 1.0;

function Awake ()
{
	Destroy (gameObject, lifeTime);
}

function OnCollisionEnter(collision : Collision)
{
	Destroy (gameObject, 0);
}

The death timer is self-explanatory, and the collision entry is so that the laser destroys itself whenever it collides with anything.

Works nicely for me, each of my lasers flies wonderfully and destroys itself as desired. Hope that works for you!

thanks for the reply but still throws up a error here is the full code if any one wishes to delve in there that would be fab

var speed = 10;//speed of bullet
var ammo:Rigidbody;//ammo
var currentBullets = 0;//This is the amount RIGHT NOW that the gun has. (should be 0 by default.)
var totalBullets = 100; //TOTAL amount the gun will have
var readynow : boolean = true;
//var emelly : GameObject;
var ammofont: GUIText;


function Start()


{
if(Input.GetKey("space"))

 

{
if (currentBullets > 0) //only shoot if you have ammo.
{
readynow = false;//can't shoot
var ammoClone : Rigidbody = Instantiate(ammo, transform.position, transform.rotation);
ammoClone.velocity = transform.forward * speed;
yield WaitForSeconds(.1);// pause between shots
readynow = true;//can shoot

currentBullets--; //subtract one
}
else //if you have less than 1 bullet
{
//perform reload animation
readynow = false;//can't shoot
//emelly.animation.CrossFade("flight");//currentBullets = totalBullets; //reset ammo count

//readynow = true;// animation over can shoot

////////


}

}

}
function LateUpdate ()
{
if(readynow)
 


{
Start();

}
} 


function Update ()
{
if (Input.GetKey ("space"))	
ammofont.text = currentBullets.ToString();

//if (Input.GetKey ("space"))	
//readynow = true;
}

I still think the key lies within this function…

function Update ()
if (Input.GetKey ("space"))	

var emellybullet = GameObject.FindWithTag("bullet");
Destroy (GameObject.Find ("bullet(Clone)"));

Hmm my suggestion would be to save the Time whn the bullet is spawned and then check if it is over 1 sec and destroy it

u can see Time class for further info…

thanks for the reply flamy will look into that tomo

FIXED HAPPY DAYS

ok managed to fix it by inserting

Destroy(ammoClone.gameObject, 1);

var speed = 10;//speed of bullet
var ammo: Rigidbody;//ammo/was rigidbody change to prefab
var currentBullets = 0;//This is the amount RIGHT NOW that the gun has. (should be 0 by default.)
var totalBullets = 100; //TOTAL amount the gun will have
var readynow : boolean = true;
//var emelly : GameObject;
var ammofont: GUIText;


function Start()


{
if(Input.GetKey("space"))

 

{
if (currentBullets > 0) //only shoot if you have ammo.
{
readynow = false;//can't shoot
var ammoClone : Rigidbody = Instantiate(ammo, transform.position, transform.rotation);
ammoClone.velocity = transform.forward * speed;


Destroy(ammoClone.gameObject, 1); 
yield WaitForSeconds(.1);// pause between shots
readynow = true;//can shoot

currentBullets--; //subtract one
}
else //if you have less than 1 bullet
{
//perform reload animation
readynow = false;//can't shoot
//emelly.animation.CrossFade("flight");//currentBullets = totalBullets; //reset ammo count
//yield WaitForSeconds(2);
//however long your reload animation is
//readynow = true;// animation over can shoot

////////


}

}




}
function LateUpdate ()
{
if(readynow)
 


{
Start();

}
} 


function Update ()
{
if (Input.GetKey ("space"))	
ammofont.text = currentBullets.ToString();

//if (Input.GetKey ("space"))	
//readynow = true;
}