reset level on collision

Ok, so what I want to do is to make it that when ONLY the player hits a specific object it reset that level. I have tried many scripts for this but can’t seem to be able to get any of them to work, they will either reset the level when any object hits it or they just don’t do anything.

Please help

-Ampler Games

Usually, you should check the player tag or name in the collision code - but be aware that there are several combinations (rigidbody hits something, character controller hits a collider, character controller enters a trigger) with different procedures.

The most common case is a character controller entering a trigger (trigger script):

function OnTriggerEnter(other: Collider){
  if (other.tag = "Player"){
    Application.LoadLevel(Application.loadedLevel);
  }
}

Another possibility is a character controller colliding with a collider - in this case the event is sent to the character controller script, thus you must check the other object’s tag or name:

function OnControllerColliderHit(hit: ControllerColliderHit){
  if (hit.transform.name == "ResetGameObject"){ // compare the object name
    Application.LoadLevel(Application.loadedLevel);
  }
}

Finally, if the character is a rigidbody, use OnCollisionEnter in the other object’s script:

function OnCollisionEnter(col: Collision){
  if (col.transform.tag == "Player"){
    Application.LoadLevel(Application.loadedLevel);
  }
}

NOTE: Remember to set the correct tag or name in the appropriate object (target object in the second case, player in the 1st and 3rd)

Where does this code go please

If anyone else runs into this,

Application.LoadLevel(int);

is obsolete, you’ll want to use:

SceneManager.LoadScene