Destroy problem - "Player" tag colliding into "Destroyable" tag will not destroy.

Hello everyone, I am relatively new to the unity scene here, although I do have plenty of coding and game dev (SourceSDK) experience. I am having an issue that I am not able to figure out.

I have been through the documentation, and plenty of tutorials, and I keep coming back to the same problem. When my main player tagged “Player” (Blue) collides with “Destroyable” (red) the destroyable should be destroyed.

My setup. I am working in 2D mobile.

I have a completely blank scene, with two identical sprites.

  • My goal is to make Blue kill red. Simple enough.

Basic Setup:
9813876--1409823--upload_2024-5-4_8-15-42.png

In game-mode, the sprites have registered collision and have even moved.
The image below is in-game, with default background. Contact has been made.

Contact being registered.
9813876--1409820--upload_2024-5-4_8-14-42.png

The next two images I posted as thumbnails as they are long images.
The “Player” tag (the blue one):


The “Destroyable” tag (the red one):

Notice the Player layer and Enemy layer.

  • In the Edit> Project settings > physics 2d > Collision layer matrix, I did enable Enemy and Player.

I have tried the appropriate combination (I believe) from research prior to this post. It is my understanding that if I want to use is Trigger, then I must use the OnTriggerEnter2D method (I am in 2D), with 2D colliders. If Is Trigger is OFF, then I use OnCollisionEnter2D. I am tried both set-ups with similar results, except, that if isTrigger is enabled, then that object no longer bounces away at collision and registers nothing in the collisions - ( is this the where I should be looking? )

So my setup has:
Is Trigger OFF and the following:

using UnityEngine;

public class CollisionDestroy : MonoBehaviour
{
    // This method is called when a collision occurs with another collider
    private void OnCollisionEnter2D(Collision2D collision)
    {
        // Check if the collided object has the tag "Destroyable"
        if (collision.gameObject.CompareTag("Destroyable"))
        {
            // Destroy the collided object
            Destroy(collision.gameObject);
        }
    }
}

Did not work. - Objects bounce

If my setup has:
Is Trigger ON and the following:

using UnityEngine;

public class TriggerDestroy : MonoBehaviour
{
    // This method is called when another collider enters the trigger collider attached to this object
    private void OnTriggerEnter2D(Collider2D other)
    {
        // Check if the other collider has the tag "Destroyable"
        if (other.CompareTag("Destroyable"))
        {
            // Destroy the other object
            Destroy(other.gameObject);
        }
    }
}
  • Objects bounce

Just to keep attempting research prior to posting, also tried both combinations of the above Trig or Collision with the method below to check if it was a hierarchy related issue.

using UnityEngine;

using UnityEngine;

public class CollisionDestroyer : MonoBehaviour
{
    void OnCollisionEnter2D(Collision2D collision)
    {
        Debug.Log("Collision detected with: " + collision.gameObject.name);

        // Accessing the Transform of the collided game object
        Transform collidedTransform = collision.transform;

        // Loop through all child objects of the collided game object
        for (int i = collidedTransform.childCount - 1; i >= 0; i--)
        {
            // Accessing each child game object
            GameObject childObject = collidedTransform.GetChild(i).gameObject;

            // Check if the child object has the specified tag
            if (childObject.CompareTag("Destroyable"))
            {
                // Destroy the child object
                Destroy(childObject);
            }
        }
    }
}
  • Objects bounce

  • I have also dragged the code snippet onto the other object, with the inverted tag logic to see if it was something weird - no.

  • I have tried one Rigidbody, and two Rigidbodies, both 2D and 3D with the appropriate collider2D/collider trigger2D/trigger isTrigger…

  • I have tried both 2D suffix, and without, coupled with the appropriate 2D or non Collider / Rigid Body combo. Each attempt going through the same steps of this post - no.

  • I have increased and decreased the collider size to extend further, or be contained within the sprites, - no.

  • I translated both players to Z=0, and main camera is defaulted at Z=-10.

  • I have moved the objects so they start far, and come in, as well as start overlapping (image above shown are overlapping) - no

  • I have attempted to use gravity / no gravity, as well as simulated masses, etc (this should be irrelevant to the point of what I am trying to do, but again, i’m going through the motions - part of learning yayy) - I should go outside.

As I wrote this post, I attempted each of these again just to confirm i’m human and I thrive on pain? lol

There are no other objects in the scene. I couldn’t figure this out on a bigger project, so I opened a new one to figure it out.

Lastly, in the two thumbnail images, you’ll see sections that are minimized or closed off, ALL settings in there are 0, with no layer overrides, etc. I know I am saying "I have tried, the appropriate…" That should be taken with more than just a grain of salt lol but I swear, from what I am reading, this should be working
insert face O_____O

Apologies on the long post, and any verbiage I may be using incorrectly. Please feel free to teach me a thing-or-two. Anyways, I just want to be thorough as I can see I am not the only one that had this issue, and once it’s solved, I’m hoping this can help.

-JB

Assuming you typed all the stuff above correctly, find out if any of it is even running!

That means it is … Time to start debugging!

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log() to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

Add debug.log(“Collision detected”) to this code (with Trigger off as you said)

using UnityEngine;
public class CollisionDestroy : MonoBehaviour
{
    // This method is called when a collision occurs with another collider
    private void OnCollisionEnter2D(Collision2D collision)
    {
       Debug.log("Collision detected");
        // Check if the collided object has the tag "Destroyable"
        if (collision.gameObject.CompareTag("Destroyable"))
        {
            // Destroy the collided object
            Destroy(collision.gameObject);
        }
    }
}

Then you can work out if the collision is occurring as you think it should be. From what you have explained, it should be working.

1 Like

I noticed your red cloud has its Rigidbody2D Collision Detection set to “Continuous”. Change that to “Discrete” (just like the blue cloud) and see if that works. If not, try “Continuous Dynamic”.

Make the collision detection less accurate wouldn’t solve any issue. Also, .“Continuous Dynamic” is 3D physics. :slight_smile:

1 Like

Thank you for the detailed post about what you tried and how the objects have been configured. The only remaining question I have is: how are you moving the objects to cause the collision to occur? Are you moving them with physics in some way?

Also, as other people have pointed out, a good first step would be to add Debug.Log at the start of the OnCollisionEnter2D/OnTriggerEnter2D functions to see if any collision is detected. I see you have this in the OnCollisionEnter2D code, but you didn’t mention if the Debug.Log executed.

1 Like