- Home /
Player colliding with object then destroying/killing Player
I'm extremely new to unity and I'm trying to create a simple prototype just using a sphere (as my player, it has a 3rd person character controller but I also attached a basic sphere collider) and cubes falling from the sky attempting to "squish" the player. I want the player to be destroyed when in comes into contact with the Cube but i'm really struggling with the scripting. At the moment i'm using this:
function OnCollisionEnter(collision : Collision) {
if (collision.gameObject.name == "Player")
Destroy (collision.gameObject);
}
But it's not working. Please help =)
Answer by DeathByWarrior · Jul 31, 2011 at 08:24 AM
Try this:
function OnCollisionEnter(collision : Collision){
if(collision.gameObject.name == ("Player")){
var playerObj : GameObject = collision.gameObject;
Destroy(playerObj);
}
}
It works for me when i tried it, add it to youre cubes falling from the sky.
Answer by rahra · Jul 31, 2011 at 08:30 AM
Thanks heaps for answering but unfortunately that didn't work =/ I've been searching for ages for an answer to this problem... by all accounts it should be working... very frustrating!