OnTrigger script help...

Hey guys,

I have a script set up on an object will tells it to replace itself with another object after 3 seconds. this works fine, but what I want to do with it is to attach the code to it that will allow it to die by being triggered by colliding with another object. Here’s the existing script:

#pragma strict

var Jack : GameObject;

    yield WaitForSeconds(3);
    KillSelf();
}

function KillSelf () {

    var JackBreak = Instantiate(Jack, transform.position, transform.rotation);

    Destroy(gameObject);
    
    }

I know it’s an OnTriggerEnter event like this, but don’t know where to put it:

void OnTriggerEnter(Collider other) {
if (other.tag == "DestroyObject")
           {}

My assumption is it goes at the top after the var: but can’t seem to get it to work.

Any help would be appreciated.

Rich

if you want it to call kill self on a collision and not after three seconds you would change your code to:

#pragma strict
 
var Jack : GameObject;

function KillSelf () {
 
    var JackBreak = Instantiate(Jack, transform.position, transform.rotation);
 
    Destroy(gameObject);
 
    }


 function OnCollisionEnter(collision : Collision) {
        KillSelf();
    }

and it doesn’t matter if your collision detection function is above or below the other code it will call it from where ever it is.

It also looks like you may only want it to KillSelf if it hits certain objects? if this is the case you can check against that as well. change KillSelf above to:

if ( collision.gameObject.tag == "DestroyObject" ){
            KillSelf();
}