How do I destroy my player on contact?

Hey, I need a little help here. I am new to scripting, but I do have a bit of experience. I’m attempting to have an Indiana Jones style level in the game I am making where the roof slowly comes down and will crush the character. Since it’s a first person game, I figured it would be easier to have the player be destroyed. For some reason, nothing in this code is working, though. The game over menu won’t even pop up. Can anyone tell me what I’m doing wrong?

using UnityEngine;
using System.Collections;

public class DestroyByContact : MonoBehaviour {

public Canvas gameOverMenu;

void Start ()
{
	gameOverMenu = gameOverMenu.GetComponent <Canvas> ();
	gameOverMenu.enabled = false;
}
void OnTriggerEnter(Collider other) 
{
	if (other.tag == "Maze")
	{
		return;
	}
	gameOverMenu.enabled = true;
}

}

2 Answers

2

You want everything that involves the collision of this falling roof inside the if-statement.

If you want to destroy a gameObject:

Destroy(this.gameObject);

And if you want to enable the gameovermenu, you need to have it inside the if-statement.

I’m new to so i may be wrong but, ill try.

  1. Is this attached to your player?

  2. If so i think the return is you problem. I dont know much about return, but i think it is asking if other.tag =="Maze" then stop, so it never makes it to gameOverMenu.enabled = true;
    the reason being that your roof would be tagged “Maze”. Try adding a ! before other.tag !other.tag == "Maze" like this.

  3. If (1) is no, then there is no reference to the player.
    If it was me, and again I’m sure there is a reason I’m wrong here, but i would attach this script to the roof like this.

    void OnTriggerEnter (Collider other){
    if (other.tag == “Player”){
    gameOverMenu.enabled = true;
    }
    }
    This asks if the game object that is colliding with the roof game object is tagged “Player” then enable the menu. Any game objects without the tag “Player” should be ignored.
    Now that would only enable your menu. The game would continue on behind it.