How to Load Level on Button Press (Door)

Hi all,

Brand new Unity user as of this week, and am working on my first game. So far I have only done what I could accomplish in the editor alone, and am now trying to learn scripting. Now, let me preface this by saying that I HAVE checked previous answers to this problem. I have tried each and every one, but with no luck.

Level 0 is a hallway. I have a game object that is a hall with doorways. I created a cube and fit it to one of those doorways. I attached a box collider and checked "Is Trigger. I want the player to walk up to this door, hit a key, and load the next scene.

I found the following script in a previous answer, but it has not worked for me. Any help would be much appreciated, and thank you for your patience.

public class Door10LoadLevel : MonoBehaviour {
    
    	void OnTriggerEnter(Collider other) {
    		if (other.gameObject.name == "Player"){
    				if (Input.GetKey ("F")){
    						Application.LoadLevel ("LevelName");
  1. Am I incorrect in assuming that this script should be placed on the door?
  2. Does the player also need a collider attached, or does the trigger recognize the player game object regardless?
  3. Does my player need a script attached?

On trigger enter is only fired the moment you enter that trigger. It only checks for a single frame, so unless you are a computer you won’t be able to press the f button.
Change it into OnTriggerStay, so that the trigger will keep checking for input for as long as the player is in the trigger.
Leave the script on the trigger you made.

On trigger enter is only fired the moment you enter that trigger. It only checks for a single frame, so unless you are a computer you won’t be able to press the f button. Change it into OnTriggerStay, so that the trigger will keep checking for input for as long as the player is in the trigger. Leave the script on the trigger you made.

Also, put a collider on your player.

edit It seems converting my comment to an answer bugged out…

First, you should use OnTriggerStay(), so the check occurs every frame. Here are the answers to your questions. :wink:

  1. You can, but since your player probably takes input, you could put it there and see if the player collides with an object tagged door.

  2. Yes, he does.

  3. No, but one object needs a rigidbody.

So basically, you need the the door and the player to have colliders, and the player to have a rigidbody. [you should be using Physics to move the player probably anyway]