x


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!

more ▼

asked Jul 01 '12 at 02:32 AM

m3m3x gravatar image

m3m3x
1 1 1 3

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Jul 01 '12 at 02:34 AM

Eric5h5 gravatar image

Eric5h5
81.5k 42 133 529

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?

Jul 01 '12 at 05:07 PM m3m3x

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.

Jul 03 '12 at 09:58 PM m3m3x
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3812
x981
x567
x202
x40

asked: Jul 01 '12 at 02:32 AM

Seen: 549 times

Last Updated: Jul 03 '12 at 10:21 PM