How to add explosion after shot

public var lifeTime : int = 1;

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

Hi guys, I created a 1st person controller with a gun, and its shooting and working fine, and killing my enemies but I want to end with an explosion after they are killed. So, I dont know exactly how to put this down in the script. Above you see my first version and after this text, my second version, it doesnt give too much error but it just isnt working… please help me.

public var lifeTime : int = 1;
var explosion : Transform;

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

	function OnTriggerEnter(hit : Collider)
	{
  	if(hit.gameObject == "Bulletspawn")
    {
    Destroy(hit.gameObject);
    var exp = Instantiate(explosion, gameObject.transform.position, Quaternion.identity);
    Destroy(gameObject);
    }
    }

it doesnt give too much error, this makes me worries a little. Do you have any errors at all? Are those warnings and you take them as errors? Or you just trying to say that there are no errors at all?


From what I can see here, the only problem is this line:

//you can't compare a gameobject with string
if(hit.gameObject == "Bulletspawn")

//Correction
//Either this (less likely)
if( hit.gameObject.name == "Bulletspawn" ) 

//or this (more likely)
if( hit.gameObject.CompareTag("Bulletspawn") )

One more thing, from this documentation, will function OnTriggerEnter () work?

Why do you have 2 trigger event functions on your turret? Consider the logic, try to read the code as the machine reads it.

There is a command called Invoke : Unity - Scripting API: MonoBehaviour.Invoke

With that you can call a function after a set amount of time :

public var lifeTime : int = 1;
var explosion : Transform;
var explosionSound : AudioClip;

function OnTriggerEnter(hit : Collider)
{
    if(hit.gameObject == "Bulletspawn")
    {
        var exp = Instantiate(explosion, gameObject.transform.position, Quaternion.identity);
        Invoke( "ExplodeMe", lifeTime );
    }
} 

function ExplodeMe () {
    audio.PlayOneShot(explosionSound);
    Destroy(gameObject);
}