Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnTriggerEnter not working at Unity3D

I'm trying to detect a ball entering throw a ring in a basketball game. I am using the following script at the ring

public class Anotar : MonoBehaviour {

    private ControlJuego control;

    void Start(){
        GameObject gameControllerObject = GameObject.FindWithTag ("ControlJuego");
        if (gameControllerObject != null)
        {
            control = gameControllerObject.GetComponent <ControlJuego>();
        }
        if (control == null)
        {
            Debug.Log ("Cannot find 'GameController' script");
        }
    }

    void OnTriggerEnter (Collision col)
    {
        control.puntuar (2);
    }

    void OnCollisionEnter (Collision col)
    {
        //control.puntuar (3);
    }
}

The ring has a box collider set as trigger to detect the OnTriggerEnter method. It also has a mesh collider to detect when the ball touch it throw OnCollisionEnter. My problem is that OnTriggerEnter is not working (used breakpoint inside and it doesn't stop). Actually OnCollisionEnter works fine. My ball has a sphere collider and both use rigidbody. Any idea?

Edit: I attach screenshots from my ball and ring enter image description here

enter image description here

like image 337
Juan Diego Lozano Avatar asked Nov 01 '25 19:11

Juan Diego Lozano


1 Answers

void OnTriggerEnter (Collision col)
{
    control.puntuar (2);
}

this will never work. OnTriggerEnter needs a Collider not an Collision. Try this:

void OnTriggerEnter (Collider col)
{
    control.puntuar (2);
}
like image 131
Rafiwui Avatar answered Nov 03 '25 09:11

Rafiwui



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!