Collision 'Death' Issue

Since I am new to the Unity engine, I apologise in advance for the noob question.

function OnCollisionEnter(collision:Collision)
{
    if( collision.gameObject.tag == "Player" )
    {
        Application.LoadLevel(0);
    }
}

This code was meant to mean that when the Player collides with the gameObject, that I placed the script inside, the game would load up Level 0 (In this case the Main Menu).
However, it isn’t working and I would really appreciate any help. Thanks in advance.

what means "is not working" ;) Nothing happens? The object you placed the script on needs an attached collider that is not set to isTrigger and the player needs to be tagged as Player (you can see this in the inspector while having the player selected).

Thanks for replying so quickly. I tried your solution but it didn't seem to work either. The capsule just collides with me it does nothing. Got any more ideas on how to fix this? Thanks

how do you control your player? The player needs a collider as well. Are the objects rigidbodies? Try to make a debug log to see if somethings wrong with the tag. function OnCollisionEnter (col : Collision) { Debug.Log("Im working and colliding with "+col.gameObject.name); } this may spam your console ;)

My Player is controlled by a Character Controller (same as the enemy) neither of them have a RigidBody component in them and I used the DebugLog but nothing came up in the console that suggested that they were colliding with anything. I experimented by adding RigidBodies to both and BoxColliders but nothing seemed to work. Any more ideas?

1 Answer

1

alright :wink:
then you need the OnControllerColliderHit function:

function OnControllerColliderHit(hit: ControllerColliderHit)
{
   Debug.Log("Woho");
   if(hit.gameObject.tag == "Player")
      Application.LoadLevel(0);
}

I converted to an answer ;)

Thanks, but should I put this on the enemy or the player? Also, do I have to attach any specific colliders or rigidbodies on the player or enemy?

OnControllerColliderHit is for charactercontrollers. I think a rigidbody is not needed, hovever I'm not 100% sure ;) Since you check for the "Player"-tag and what I get from your question you would put this on your enemy. You need colliders on both. It won't work if both are meshcolliders. Also they should not be setup as triggers