- Home /
nothing happens on collision ever
script:
var health = 60;
var something : GameObject;
function OnCollisionEnter(thing : Collision) {
if (thing.collider.tag == "Bullet") {
health -= 1;
}
else
if (thing.collider.tag == "FlameBall") {
health -= 3;
}
else
if (thing.collider.tag == "Bomb") {
health -= 6;
}
}
function Update () {
if (health <= 0)
Destroy (something);
Debug.Log(health);
}
nothing happens on collision, health always stays at 60 also i have never been able to do a collision properly in the 1 1/2 months i have had unity. the script is attached, something is assigned, the projectiles have sphere colliders and the target has a rigidbody. the console says there are no errors.
Answer by aldonaletto · Oct 19, 2011 at 12:39 PM
This script works fine. I suspect you're attaching the script to the wrong thing: it must be attached to the target object, thus the variable something is not needed - you should destroy gameObject instead, which references the object itself:
var health = 60; // you don't need the "something" variable!
function OnCollisionEnter(thing : Collision) { if (thing.collider.tag == "Bullet") { health -= 1; } else if (thing.collider.tag == "FlameBall") { health -= 3; } else if (thing.collider.tag == "Bomb") { health -= 6; } // Debug.Log is more useful here: Debug.Log("Tag:"+thing.collider.tag+" Health:"+health); }
function Update () { if (health
the scripts are attached right, the target has like 30 colliders and a rigidbody, the bullets have 1 child collider and a rigidbody, also in the hierarchy, the bullet that is colliding is called Bullet(Clone)
30 colliders? Try with a simpler target - a cube, for instance - then increase the complexity to find what's causing the problem. I tested the script above attached to a simple cube, and the cube health decreased as expected when shot. I added another cube as a child, and it still worked as if both were a single body.
i removed every collider except the parent collider and still the same result
i skipped the update to 3.4 because my internet was dead and i have 3.3, how do i update?
Your answer
![](https://koobas.hobune.stream/wayback/20220613013902im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Change Direction on Collision 6 Answers
get angular velocity change from collision 0 Answers
Collision with no contact? 2 Answers
script trying to access a null gameobject's collider... can't fix 2 Answers