Nothing Happens at OnTriggerEnter ?

I’ve got a problem about OnTriggerEnter;

#pragma strict

static var health : int = 100;

var bullet : GameObject;

var explosion : GameObject;

function Start () {

bullet = GameObject.FindWithTag("Bullet");

}

function OnTriggerEnter (bulletCol : Collider)
{

if(bulletCol.gameObject.name == bullet.name)
{
health -= 50;
}

}

function Update () {

if(health < 5)
{

	Instantiate(explosion, transform.position, transform.rotation);

}

}

This is my code, but when a bullet hits, it doesn’t affect. I tagged bullet prefab as “Bullet” but nothing happens. so what can i do ?

Try to change FindWithTag to CompareTag and try

Hi,

there is the chance you forgot to add a rigidbody-component to either your script’s gameObject OR (just one is okay) the Bullet itself?
Also your Trigger needs a Collider marked as Trigger and also your Bullet needs a Collider.

Cheers

Select the Collider you are coding for and mark the “Is Trigger” box in the inspector.