Trouble destroying instantiated object

I am instantiating a projectile and trying to have it delete after 3 seconds. I have been using this script:

#pragma strict

var prefabBullet : Rigidbody;
var shootForce : float;
var shootPosition : Transform;


function Update () {

	if(Input.GetMouseButtonDown(0)){
		var instanceBullet = Instantiate(prefabBullet, transform.position, shootPosition.rotation);
		instanceBullet.rigidbody.AddForce(shootPosition.right * shootForce);
		Destroy(instanceBullet, 3.0);	        
	}
}

…But it doesn’t work. I don’t know why it isn’t as I have not done much programming, and I haven’t done it in a long time (took a big break from Unity).

I have 2 cannons, that both instantiate/clone the same projectile and shoot it. I have tried other methods such as using Destroy(gameObject.Find(“projectile”)), which did seem to do the job in a way, but for some reason it would only delete one of the two instantiated projectiles.

Any help is appreciated!

This is the correct way to do it. Instantiate will always return an Object, not a GameObject, so you might be having a typing issue (Hence you should use C# and not JavaScript.) Try the following edit:

var instanceBullet = Instantiate(prefabBullet, transform.position, shootPosition.rotation) as GameObject;

create a script and write the destroy cod in it’s Awake function
then attach it to your projectile prefab.
in this way when your projectile instantiate the awake function of it will run which will destroy it after the given time.

write this cod in destroyer script attached to your projectile
I hope it gonna works.

var delay : flaot; // your prefab lifetime

function Awake()
{
 Destroy(transform,delay);
}

I have fixed it finally! After the advice, I tried not using .Find, so I tried to continue the use of Destroy(instanceBullet, since I have almost no knowledge on arrays. Adding "as GameObject also didn’t work.

The solution was to change:

Destroy(instanceBullet, 3.0);

to:

  Destroy(instanceBullet.gameObject, 3.0);  

Thanks for all of the advice too!