How do you make a collision based fps script that deletes the object 2 seconds after it is fired?

Here is my current code:

#pragma strict
var prefabBullet:Rigidbody;
var shootForce:float;
var shootPosition:Transform;
function Update()
{
	if(Input.GetButtonDown("Jump"))
	{
	var instanceBullet = Instantiate(prefabBullet, transform.position, shootPosition.rotation);
	instanceBullet.rigidbody.AddForce(shootPosition.right * shootForce);
	}}

I would like to add something to this script that would delete the bullet that is shot after 2 seconds.

2 Answers

2

Add the script below to your prefabBullet game object prefab.

#pragma strict

function Start()
{
    Invoke("DestroyBullet", 2);    // 2 is the time after which bullet is to be destroyed.
}

function DestroyBullet()
{
    Delete(gameObject);
}

It is really easy to do in C# for people that want it

Public GameObject bulletObj;

void Start()
{
Destroy (bulletObj, 2);
}

Really simple