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);
    }
    }

Try changing explosion from transfOrm to a game object

I don't think that matters. Instantiate() requires a Object as the parameter, not GameObject. http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html

2 Answers

2

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?

@Chronos-L I am not really much into scripting yet.. but I am working on it. My FPC shoots the Turret and its killed, so thats fine, it works with this code: public var lifeTime : int = 1; function OnTriggerEnter () { Destroy(gameObject, lifeTime); }

@Chronos-L But I want to let it explode, not just dissapear, because that just looks lame somehow..

Does fixing the if( hit.gameObject == "BulletSpawn" ) fix the problem? If not, for your second script, try keeping the function OnTriggerEnter( hit : Collider ) and remove the function OnTriggerEnter().

@Chronos-L I am not sure what you mean, I already tried what you wrote above and nothing seems to happen. What do I have to do with "if" and what do I have to try with function OnTriggerEnter? If I remove it it gives a huge error..:(

From your code, you will instantiate a explosion if the condition is right. So the problem now should be your setup. Is this script attached to a enemy or a bullet? And what errors you are getting? What does your current script look like?

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);
}

@alucardj Hey, thanks for all your help, and sorry for my late reply. And indeed there was some misunderstanding. Also sorry for that, and cheers ;)

I used the invoke function and it worked. Thank you!