1
votes

Dear sctackoverflow Community,

I've got a problem with the onTriggerEnter2D function in unity3D, since it's not working, and at the same time, I'm not aware of what is going wrong. Note that I have searched this problem in other forums and threads, and none of the solutions would work for me.

So, basically what I've got is two 2D-objects (sprites) and both of them got aswell a rigidbody2D as a circle collider 2D. I have 'is Trigger' checked on both of them, and I've got 'is Kinematic' unchecked in both 2D rigidbodys. Also I've tried all sorts of possible combinations, testing around with the colliders and rigidbodys.

Now my code is this (JavaScript):

    #pragma strict


var velocity : Vector2;
var objectInReach : boolean = false;

function Update () {

transform.position = transform.position + (velocity * Time.deltaTime);


}

function onTriggerEnter2D(other: Collider2D)
{
    objectInReach = true;
    Debug.Log(objectInReach);
    Debug.Log("collision!");

    if(other.gameObject.tag == "player" || other.gameObject.tag == "buttonSprite")
    {
        Debug.Log("collision!");
        Debug.Log(objectInReach);
    }

}

(My moving object is tagged "player", and my static object is tagged "buttonSprite") (The script is attached to the moving sprite, tagged "player")

You can see that by now I'm desperately trying multiple things out in my code.

Please, if you know what I'm doing wrongly, let me know.

1

1 Answers

1
votes

It's your Typing.... Change onTriggerEnter2D to OnTriggerEnter2D