- Home /
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.. :(
#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? :)
Sorry if I made any typos.. I am little tired. It's like 3 Am here.
No, that wasn't the problem. Anyway, case closed. :))
This is sort of obvious, but might as well check....
Did you add the scenes in Build Settings?
Answer by hexagonius · Oct 24, 2016 at 07:23 PM
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
Answer by NotEnoughLemons3D · Oct 24, 2016 at 07:22 PM
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!
Answer by ZaninDevelopper · Oct 25, 2016 at 05:33 AM
"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.
Answer by TheEngine · Oct 25, 2016 at 07:11 PM
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! :D
Your answer
Follow this Question
Related Questions
Changing Rate of Change from Key Key Input Values 1 Answer
access Input.GetButton key name 1 Answer
[ASNWERED] How To Change Button To An Alphabet Key 2 Answers
GUI Box Doesn't stay open 1 Answer
Get numeric key 4 Answers