0
\$\begingroup\$

I checked the other answers for this but mine didn't get fixed Here's the code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DestroyCollectibles : MonoBehaviour
{
    private void Start()
    {
        Debug.Log("Hallo");
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        Debug.Log("Trigger");
    }
    private void OnCollisionEnter2D(Collision2D collision)
    {
        Debug.Log("Collsion");
        // if (collision.gameObject.CompareTag("Collectible"))
        // {
        //     Debug.Log("Apple");
        //     Destroy(collision.gameObject);
        // }
    }
}


And screenshots:

Destroyer collectible

\$\endgroup\$
4
  • \$\begingroup\$ Missing one rigidbody2d on one object \$\endgroup\$
    – Zibelas
    Feb 9, 2021 at 7:53
  • \$\begingroup\$ Ohhh So i need a rigid body \$\endgroup\$ Feb 9, 2021 at 8:04
  • 1
    \$\begingroup\$ OnTriggerEnter is called if your BoxCollider2D (the one which something should enter) is marked as trigger. And as Zibelas said, you are missing a RigidBody2D on one object (the one that is entering). See the note in the docs: docs.unity3d.com/ScriptReference/… or docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html \$\endgroup\$
    – D.Kallan
    Feb 9, 2021 at 8:12
  • 1
    \$\begingroup\$ That looks like it would make a good answer. \$\endgroup\$
    – DMGregory
    Feb 9, 2021 at 9:20

1 Answer 1

1
\$\begingroup\$

OnTriggerEnter() is called if your BoxCollider2D (the one which something should enter) is marked as trigger. And as Zibelas said, you are missing a RigidBody2D on one object (the one that is entering).

A quote from the Unity documentation on MonoBehaviour.OnTriggerEnter2D():

Note: Trigger events are only sent if one of the Colliders also has a Rigidbody2D attached. Trigger events are sent to disabled MonoBehaviours, to allow enabling Behaviours in response to collisions.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .