- Home /
Is there a way to combine the Update and OnGUI functions?
I need to combine them so that my GUI constantly updates.
private var IsRKeyPressed : boolean = false;
private var IsRocksButtonPressed : boolean = false;
function Start () {
}
function Update () {
//Is R key being held down
if (Input.GetKey (KeyCode.R)) {
IsRKeyPressed = true;
} else {
IsRKeyPressed = false;
}
//Opens Rock Menu
}
function OnGUI () {
if (IsRKeyPressed == true){
//Rock Category (With rock items/models)
if (GUI.Button (Rect (0,20,100,100), "Rocks")) {
IsRocksButtonPressed = true;
if(IsRocksButtonPressed == true) {
if (GUI.Button (Rect (200,50,100,100), "Small Rock")) {
}
}}}}
We need to see your code to understand why you are having a problem. You can certainly set variables in Update() that are read in OnGUI(). You cannot combine the two functions. Also note that OnGUI() gets called multiple times per frame for different events.
I just put in the code. I am trying to make a menu where I can spawn in custom things for a game engine im working on. I can't get the if(IsRocksButtonPressed == true) { if (GUI.Button (Rect (200,50,100,100), "Small Rock")) { }
part working. When I click the button, the other button won't pop into the GUI.
Can someone please answer? I really need help with this.
njpatter,
I think he means for the R button to have to be held down. As referenced in his update function:
//Is R key being held down
Answer by njpatter · Feb 07, 2014 at 07:48 PM
Your code requires that you press the R key to make a GUI.Button show up, which you will need to continually hold down to get another button to show up... so you will never be able to click the second button because if you let go of the first the second will disappear.
I'm a c# guy, but maybe try something like....
function OnGUI () {
if (IsRKeyPressed == true){
if (GUI.Button (Rect (0,20,100,100), "Rocks")) {
IsRocksButtonPressed = true;
}
}
if(IsRocksButtonPressed == true) {
if (GUI.Button (Rect (200,50,100,100), "Small Rock")) {
//Do Something here
isRocksButtonPressed = false;
}
}
}
This way you don't need to hold the R button and the first GUI button after you have enabled isRocksButtonPressed
I took the liberty and converted your comment into an answer.
I think he wants to have to hold down the R button to show the GUI. The second if (IsRocksButtonPressed) should be inside the first IsR$$anonymous$$eyPressed-if but of course outside of the button-if which is only executed once when you click the button.
Your answer
Follow this Question
Related Questions
Assign keystrokes to GUI button 2 Answers
gui button to work exctly like up arrow key 1 Answer
Activate GUI Button with Enter Key 1 Answer
How will i develop the type of gui? 0 Answers