- Home /
Trigger GUI with a key when colliding (C#)?
Hey all,
I'm completely new to scripting and Unity, and I have to finish a Unity project as part of a module in my Master's course. The logic of scripting still seems to escape me, I can execute all sorts of simple commands but when it comes to combining many different commands into one script I'm lost. Can anyone help me with the following scenario? It would have to be in C#; I've seen some solutions to this in Java but our projects need to be in C, and I just don't have the skills to translate from one language to another. Also I know the new UIs have come with the new update and that they're more intuitive to use, but we aren't allowed to use them either, I'm using Unity 4.3 atm.
My game is a simple educational explore-an-environment-and-click-on-things-to-learn-about-them. I have multiple different objects to explore, all with different GUIs. I want to have
1) a GameObject in my scene that when the player collides with it, a question mark GUI appears to let them now it's possible to investigate the object,
2) and then when they press key X when on this collider area a GUI window with text pops up,
3) when they press X again the window disappears,
4) When they leave the collider area the question mark GUI disappears.
I can make the texture appear and disappear when the player collides with the trigger, I can toggle GUI windows, I can trigger events with the pressing of a key - but I can't get my head around how to combine these things, as in, what needs to be void onTriggerEnter, what on void Update, what on onGui etc. etc... How to make the condition that this specific GUI will appear ONLY if the player is on this specific area and ONLY if she presses X? Any help would be greatly appreciated!
Answer by Baste · Dec 05, 2014 at 02:26 PM
You'll need to use some bools to set the state of your script. Something like:
bool playerInTrigger;
bool showWindow;
void OnTriggerEnter(Collider c) {
if(//check if the Collider c belongs to the player here. If only the player moves, skip the if) {
playerInTrigger = true;
}
}
void OnTriggerExit(Collider c) {
if(//same check as in OnTriggerEnter) {
playerInTrigger = false;
showWindow = false;
}
}
void Update() {
if(playerInTrigger && Input.GetKeyDown(KeyCode.X)) {
showWindow = !showWindow;
}
}
void OnGUI() {
if(showWindow) {
//Draw the window here
}
}
This is pretty basic stuff. I'd look into some more tutorials if I were you! But the skeleton above should get you started; you can only open the window if the player is in the trigger, and if you leave the trigger area, the window closes.
About where to put what - OnTriggerEnter fires when you enter the trigger, and only then. OnGUI and Update fires once every frame. If you put GUI code in Update, Unity complains and refuses to draw your GUI. You could theoretically put input code in OnGUI, but oh God why would you it's called OnGUI.
Rant:
I will have to keep on saying this over and over again to my dying day, but Java and Javascript are not the same thing. Not even remotely close. When you're saying:
"It would have to be in C#; I've seen some solutions to this in Java but our projects need to be in C"
There's no Java in the Unity world - only a variant of Javascript. They're NOT THE SAME - Java is a lot more similar to C# than it is to Javascript. Also, C and C# are about as different as elephants and tax reform, so your project definitely does not need to be in C.