What is wrong here?!

Hey guys!
So I just made a simple script wich I want to use in a 3D game.
IDK what the heck is wrong with it… I mean The GUI works perfect but I keep pressing the “E” on my keyboard and it won’t load the next scene… :frowning:

#pragma strict

public var scene : String;

var ShowInstructions : boolean;
var stringToEdit : String = "Press E to enter!";

function OnTriggerEnter(col: Collider)
 {
	 ShowInstructions = true;
	 if(gameObject.tag == "Player" && Input.GetButtonDown("E"))
	 {
         Application.LoadLevel(scene);
     }
 }
 
 function OnTriggerExit(col: Collider)
 {
	 ShowInstructions = false;
 }
 
 function OnGUI()
 {
	 if(ShowInstructions == true)
	 {
		 stringToEdit = GUI.TextField(Rect(10,10,200,20), stringToEdit, 25 );
	 }
 }

And yeah, my FPS player is tagged as player and i marked the collider on the door as “IsTrigger”.
Anyone? Pls? :slight_smile:

**Sorry if I made any typos… I am little tired. It’s like 3 Am here. **

The function OnTriggerEnter is called once when the an object enters the trigger. Input.GetButtonDown also only returns true ONE time. Therefore it is almost impossibly to have the player press ‘E’ and enter the trigger in the exact same frame (say your game is running 60fps).

What you should do instead is use Input.GetKey(KeyCode.E).

That should work because Input.GetKey() stays true as long as key is held down and will go across multiple frames.

Good Luck!

you made it impossible to get the level loaded. the moment OnTriggerEnter is called, also keydown has to be true… no chance.
I don’t know what exactly your use case is, but make it a getkey if it should fire when triggering while holding E, or put the key code into an Update function

“OnTriggerEnter” and “Input.GetKeyDown” both fire for only 1 frame, therefore hitting both at the same time is pretty much impossible. So you need to:

Change from “OnTriggerEnter” to “OnTriggerStay”. This will allow you to press the button down and load the next scene whenever the player is inside the trigger collider. However, this only works if you press the key down after the player enters the trigger.

You can also change to “Input.GetKey”. This way, if you were holding the key down while the player entered the trigger, the scene will be loaded as soon as the player enters it.

Note that if you only change to “Input.GetKey”, you will need to be holding down the E key when the player enters the trigger, since “OnTriggerEnter” only fires for 1 frame.

Tip: Change from “Input.GetButton” to “Input.GetKey”, it’s easier to use and you can use KeyCodes with it. Read on it.

Now it works!

#pragma strict

public var scene : String;

var ShowInstructions : boolean;
var stringToEdit : String = "Press E to enter!";

function OnTriggerEnter(col: Collider)
 {
	 ShowInstructions = true;
 }
 
 function Update()
 {
	 if(Input.GetKey(KeyCode.E))
	 {
		 Application.LoadLevel(scene);
	 }
 }
 function OnTriggerExit(col: Collider)
 {
	 ShowInstructions = false;
 }
 
 function OnGUI()
 {
	 if(ShowInstructions == true)
	 {
		 stringToEdit = GUI.TextField(Rect(10,10,200,20), stringToEdit, 25 );
	 }
 }

Thank u all for fast answering. Have a nice day! :smiley: