Destroy on collision

I know many threads like these were made , but I cant seem to make it work

So this script will be attached to an object which is destroyed after touching the “Player”

using UnityEngine;
using System.Collections;
 
public class TouchDestroy : MonoBehaviour 
{
 
 
void OnCollisionEnter(Collision collision)
{
    if(collision.gameObject.name == "Player"){
			Destroy(collision.gameObject);
		}
	}
}

Destroy(this);

your are destroying the player not the object.

ahem Destroy(gameObject);

Destroy(this) would destroy the script from the gameObject (ie the script component).

1 Like

lol ups ^^

  using UnityEngine;
    using System.Collections;
     
    public class TouchDestroy : MonoBehaviour
    {
     
     
    void OnCollisionEnter(Collision collision)
    {
        if(collision.gameObject.name == "Player")
       {
                 Destroy(this.gameObject);
            }
        }
    }

Did that script work? I’m not a c# guy cause it makes my eyes bleed. If it didn’t work , then I wonder if you reference the object first.

(eg)

if(collision.gameObject.name == “Player”)
var Obj = collision.gameObject;
Destroy(Obj);

Nope it doesnt seem to work… I added the script to a basic sphere for tests …But on collision nothing happens…
Maybe a public variable would make any difference?

onCollisionEnter requires rigidbodies things…

That was the mistake , now its working …Thank you very much all of yo1u