- Home /
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 ?
Answer by CrilleStyles · Feb 07, 2014 at 03:11 PM
Try to change FindWithTag to CompareTag and try
Answer by Dennis_eA · Feb 07, 2014 at 12:13 PM
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
Answer by meat5000 · Feb 07, 2014 at 12:08 PM
Select the Collider you are coding for and mark the "Is Trigger" box in the inspector.
Your answer
Follow this Question
Related Questions
Reaching a gameobject from its collider 3 Answers
Trigger can't see the enemy 2 Answers
Help with collide triggers and calls, please. 1 Answer
Activate/deactivate all GameObjects with same tag c# 1 Answer
Undefind tag? 0 Answers