A
A
Art77772021-01-05 20:20:24
Unity
Art7777, 2021-01-05 20:20:24

How to handle object collision in C#?

Can you please tell me how to compare tags of colliding game objects from different arrays (lists) in C#?
I need to write a code that, when colliding, calculates whether the colliding objects have the same tags, if they are the same, then one action, if not, then another
Ps I am a beginner
I work in Unity, objects collide through the OnCollisionEnter () function

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
alfss, 2021-01-05
@alfss

I propose from the beginning to answer the question - how is it determined that the objects collided?

C
Chalaiyev, 2021-01-05
@Chalaiyev

If I understand correctly, the code should look something like this:

private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "Тег объекта")
        {
            
        }
        else
        {

        }
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question