- Home /
Is there something wrong with my c# script?
I Can't seem to figure out what the issue is here. Once i play the game, I go to my trigger collider, the GUI message appears, but when I click 'E', I get a "key not mapped" error. I tried swapping "E" for "KeyCode.E". But then I just don't get anything once I click "E".
using UnityEngine;
using System.Collections;
public class Scene_Prctice : MonoBehaviour
{
public bool showGUI = false ;
//transition to next scene/button show/button press
//
//
//
//
void OnTriggerEnter (Collider other) //char enters collision trigger
{
if(other.CompareTag("Player")) //if char tag is "player"
{
showGUI = true;
}
if (Input.GetKeyDown(KeyCode.E)) //= "Press 'E' To Enter";
{
Application.LoadLevel("noWhere");
}
}
public void OnTriggerExit (Collider other) //char enters collision trigger
{
if(other.CompareTag("Player")) //if char tag is "player"
{
showGUI = false;
}
}
//Application.LoadLevel ("noWhere"); //Loads the level
public void OnGUI()
{
if(showGUI)
{
GUI.Box (new Rect(400,200,200,20), "Press 'E' To Continue");
}
}
//
//
//
//
//
//
}
Answer by screenname_taken · Jun 12, 2014 at 05:21 AM
Your problem is that you have the check for the button in the ontriggerenter function. That function is a one-off trigger. It's buff and gone. So unless you manage to press "e" on the same frame that the function triggers you won't get anything.
It goes "Something's in! brrrr check everything once. Done..."
Instead move your if (Input.GetKeyDown(KeyCode.E)) //= "Press 'E' To Enter"; { Application.LoadLevel("noWhere"); }
to Update() so that it is able to check for it every frame till you press it and add a boolean check with && next to (input.getkey....E) and make the boolean true in that OnTriggerEnter() function.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Can't draw a box using Basic GUI 1 Answer
Leaderboard GUISkin? 1 Answer
Displaying a static variable from another script with OnGUI 1 Answer
NullReferenceException problem 2 Answers