Destroy Object On Collision: C#

hello,

trying to set up a basic collision with my player and an artefact. the artefact must be destroyed upon the players collision. Id prefer it if it could be a trigger i.e. if input.getkey(“action button”) within the triggerbox it gets taken and therefore destroyed… but its far beyond me at the moment.

using UnityEngine;
using System.Collections;

public class ObjectCollection : MonoBehaviour 
{
	public AchievementManager AM;
	
	void OnCollisionEnter(Collision Collection)
		{
		if(Collection.gameObject.name == "Artefact")
		{
			AM.OnAchievementWon("Achievement2");
			Debug.Log("Congratulations - You Collected 1 / 10 ArteFacts!");
			Destroy(Collection.gameObject);
		}
	}
}

Code is seems fine. But it better not to compare by gameobject.name, use tags instead.

And describe problem wider. As I sad this piece of code is seems fine.
So problem is: Collision is not trigering? Or your Artefact is not destroying? Or you have other error?

Collision isnt doing anything, nothing happens be that the debug or the destroy. its literally acting asif nothing is happening.

If you set you collider as trigger, check this out, it should help.

that worked great thanks man :slight_smile:

Sorry to bring up an old thread, and I feel really stupid, but how do I specify what Game Object is destroyed? I’ve tried gameObject.tag but I don’t know what to do.

i have the same problem,lol

Destroy simply just destroy a game object.
In case you want to destroy the game object that is attached to this script you can just call

Destroy(gameObject);

If you want to Destroy something else you need to get a reference for it.
e.g; the OnCollisionEnter method, you get a Collision.
You can do something similar to what OP did, and call .gameObejct on the Collision variable to send a reference to that game object that collided with the game object that is attached to this script.