- Home /
Text Prompt Problem
I'm having a problem trying to get this object to be interactive in this environment I am building. When you approach a sign with this "wanderer_figure" I want a prompt to pop up within the GUI layer asking the player to press space to activate further text to be shown. Right now I have it coded as such:
var showGUI : boolean = false;
var customGUI : GUISkin;
function OnTriggerEnter (myTrigger : Collider) {
if (myTrigger.gameObject.name == "Wanderer_Figure"){
showGUI = true;
}
}
function OnTriggerExit (myTrigger : Collider) {
if (myTrigger.gameObject.name == "Wanderer_Figure"){
showGUI = false;
}
}
function OnGUI() {
//activate custom skin settings/textures
GUI.skin = customGUI;
if(showGUI){
GUI.Box (Rect (Screen.width/2-100, Screen.height/2+200,200,100), "Press SPACE to Interact");
}
if (Input.GetKeyDown ("space")){
showGUI = false;
GUI.TextField (Rect (Screen.width/2-100, Screen.height/2+200, 400,200), "TEST");
}
}
However, when I try and test this code, the second half of the GetKeyDown call doesn't work, even if I hold down the space key. I'm trying to see what the problem, and also am wondering how I might make this second set of text disappear after a certain period of time (or else disappear based on more user input).
I'm kind of a n00b at this, so any help would be greatly appreciated, and thorough explanation of any tips would would equally be extremely helpful!
Answer by Eric5h5 · Jul 01, 2012 at 02:34 AM
GetKeyDown only returns true for one frame, so the textfield will only appear for one frame. Although GetKey shouldn't be used in OnGUI, use Event.current.type == EventType.KeyDown instead. You can use that to toggle a boolean which would dictate whether the textfield is used or not.
Actually this advice is not good, and when I tried to implement this in the OnGui is gives me an error. I'm not sure what you are suggesting me to do, but how would you fix the above code using Event.current.type?
Also I'm having trouble trying to get this code to work only when you are within the collision limits. As it stands right now, regardless of where I stand, if I press the space bar the prompt will become activated, but I want to only allow for this to be the case when you are within the boundaries of this empty game object.