R
R
Roman Pugachev2019-12-04 17:59:21
C++ / C#
Roman Pugachev, 2019-12-04 17:59:21

Why do events float on contact with a trigger in Unity 2d?

Hours 6 already stuck on the spot.

void OnTriggerEnter2D(Collider2D other)
    {
        string a = other.gameObject.tag;
        switch (a)
        {
            case "Right":
                Debug.Log("Right answer");
                break;
            case "NotRight":
                Debug.Log("Not right answer");
                break;
        }
        
    }

I have an object with a trigger, and if other objects touch it, then I need to output the result to the console. (Right answer/Not right answer). But everything floats when touched, if it is an object with the "Right" tag, then it is written twice in the console that the correct option, and if it is an object with the "NotRight" tag, then two messages appear in the console at the same time - that the option is correct, and that it is not correct. I assigned tags to objects, made the main object a trigger through box collider, already rewrote it under if-s, and tried some kind of left function Compare.Tag, RigidBody seems to be there too - everywhere there is one result. Why are they performed together and what the hell is this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Gaydak, 2019-12-05
@MrMureno

it all sounds like you have not one single object touching the trigger, but a whole bunch.
make a mini scene. where ONLY ONE trigger and ONLY ONE object that touches it and NO extra scripts and logic.
make sure that all this works, and then see what was wrong with you in the scene (maybe turn off the object, maybe create objects somewhere or something else that makes the trigger react)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question