Y
Y
yraiv2022-02-08 16:32:44
Unity
yraiv, 2022-02-08 16:32:44

Why doesn't object collision check work?

2d game. I want to prohibit placing objects on top of each other. After a person buys an object, he moves after the cursor while the mouse is held down, and when he releases it, he leaves it there, but! I want it to be impossible to put them on top of each other and that's the problem. Decided to do this, but nothing works

void OnTriggerEnter2D(Collider2D collider)
    {
        if (collider.gameObject.tag == "Towers")
        {
            Destroy(TowerPistol);
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mimocodil, 2022-02-13
@yraiv

Checklist to check:
1. Is there a suitable tag on the object?
2. Is there a collider on the object?
3. Is the isTrigger checkbox enabled on at least one of the two colliders? (since OnTriggerEnter2D)
4. Are the colliders in the same plane (usually Z)?
5. Does the script, a fragment of which is indicated in the code above, hang on the object that is being placed?
6. Is the TowerPistol reference stored null?
7. Is the link (TowerPistol) a valid link to the correct object?
If a Rigidbody hangs on the object, you can try changing Collision Detection to Interpolate.
If everything checked out and the problem persists, try changing the OnTriggerEnter2D method to OnTriggerStay2D.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question