Destroy script turns objects into ghosts

Hello.
I have been researching all day, and I still can’t find any answers for this. As implied, my destroy script has not destroyed the game objects. Instead, their collisiders get removed, and they can fly around like phantoms. I am trying to destroy two 2D objects, each with this script. Any advice on what I’m doing wrong?

Thanks in advance. Here is the script.

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

public class DestroyCollision : MonoBehaviour

{ 
    // Start is called before the first frame update
    void OnCollisionEnter2D(Collision2D other)
    {
        //Check the provided Collider2D parameter other to see if it is tagged "PickUp", if it is...
        if (other.gameObject.CompareTag("GameController"))
        {
           
           Destroy(gameObject);
        }
    }
}

It is directly attached to the gameobject which is to be destroyed.

I’m basing it on behaviour, but now I have a new problem. I did change my code so I would use the same code for the object to be destroyed, and now my app crashes instead of just causing the issue.

Here’s the updated script.

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

public class MovementMinus : MonoBehaviour
{
    public float movespeed;

    // Update is called once per frame
    void Update()
    {
        transform.Translate(movespeed * SimpleInput.GetAxis("Horizontal") / 10, movespeed * SimpleInput.GetAxis("Vertical") * -1 / 10, 0f);
    }


    // Start is called before the first frame update
    void OnCollisionEnter2D(Collision2D other)
    {

        //Check the provided Collider2D parameter other to see if it is tagged "PickUp", if it is...
        if (other.gameObject.CompareTag("GameController"))
        {
            Destroy(gameObject);
        }
    }
}

It’s weird. It shows no message after it crashes. Just a Unity logo with an error sign for about a second, and then nothing.

Sorry. The editor is crashing when the object to be destroyed collides with the destroyer.

Hi, you script seems to be correct, but what happens is that it will only destroy the GameObject this script is attached to (and consequently all of it’s children) but not any parent G.O.

I guess your collider and render are two distinct Game Object with the collider being children from the render, in this case you’d need to make a reference to the root GameObject in your script and destroy that one instead.

After a long while, I found out how to do it. Instead of having the object destroy itself, I set up the collider to trigger Destroy.

If case anyone else has the same issue I did, here is the code I used.enter code here. I have a debug.log test which I used to test the trigger still attached.

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

public class Instantiator : MonoBehaviour
{
    int magnet = 2;


void OnTriggerEnter2D(Collider2D other)
{
    //Check the provided Collider2D parameter other to see if it is tagged "PickUp", if it is...
    if (other.gameObject.CompareTag("Player"))
    {
       magnet = magnet - 1;
            string converter = magnet.ToString();
            Debug.Log(converter);
            Destroy(other.gameObject);
    }
}
}