Delete object on touching it

I’m trying to have a ‘key’ which is a object in the game, that when i touch it, it will make “GotKey = 1” and delete it from the scene

var GotKey : boolean = false;



function OnCollisionEnter(col : Collision){



	if(col.gameObject.name == "Key"){

		Destroy(gameObject);

			GotKey = true;
	}
}

i put this on the Player, is this wrong?, it;s not working

or DestroyImmediate:wink:

But doesn’t my code do that already?, I’m confused

Be sure your objects has Rigidbody

I forgot to add a RigidBody, But now i have it’s still not working, To confirm, this code Does go on the Player right?, and not the Key

Try this but note: " Key object, check on is Trigger

// Destroy everything that enters the trigger

function OnTriggerEnter (other : Collider) {
    Destroy(other.gameObject);
}

I would also make sure that both objects have colliders that are enabled. If they do, then I would put a debug in to see what name you are getting for the gameobject (check to make sure the case is correct, etc) in the collision event.

Debug.Log(col.gameObject.name);

Debug.Log(col.gameObject.name); Come back as Character controller

Hey there, if you are instantiating the object you are trying to destroy, its name is probably something like so: “Key (Clone)”. In which case its obviously not going to work.

It’s not an instantiated object, yet, But the Capsule colider is colliding with the Character controller first, not the Key, i think

Wait, the key has two colliders? Why?

The key has one Sphere colider, my Character has a character controller, and a capsule collider, and the Capsule colider of the character controller, collides with the Character collide, not the sphere collider of the key

Argh, you don;t need the Collider, he character controller has a colider doesn’t it?,

True, you could use: Unity - Scripting API: CharacterController.OnControllerColliderHit(ControllerColliderHit)

thanks