Baalhug
1
Hi, guys:
I’m having a problem with buttons inside a GUI Window. I want a message appears when the player clicks a button to remind him he has chosen that option, but i dont want to use option buttons, just normal ones. So i got this code:
/*EDIT:*/ string txt_menu1="";
void OnGUI (){
GUI.Window(1, new Rect(20, 20, 180, 300), menu1, "Menu One");
}
void menu1(int windowID) {
if (GUI.Button(new Rect(10, 20, 160, 50), "Choose option 1")) {
UseType1=true;
txt_menu1="You have chosen Option 1";
}
GUI.Label(new Rect(10,200,160,90), txt_menu1, "Box");
}
This is not working. When i press the button nothing happens. I’m guessing the function menu1 is only called on creation though i’ve seen in the documentation some examples where the code is written there, in the Window creation function, same for the text to be shown (txt_menu1). What am i doing wrong?
Seems to work just fine here (4.3.1f1)
@ArkaneX is probably right in that you are setting the variables elsewhere.
(I also obtained a new ID from GUIUtility rather than passing it a magic number.)
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour
{
public bool UseType1;
public string txt_menu1;
void OnGUI ()
{
int id = GUIUtility.GetControlID(FocusType.Passive);
GUI.Window(id, new Rect(20, 20, 180, 300), menu1, "Menu One");
}
void menu1 (int windowID)
{
if (GUI.Button(new Rect(10, 20, 160, 50), "Choose option 1"))
{
UseType1 = true;
txt_menu1 = "You have chosen Option 1";
}
GUI.Label(new Rect(10, 200, 160, 90), txt_menu1, "Box");
}
}
If i use GUIUtility it works. Gonna check that function because i have 2 windows (ids 1 and 2 mannually) and it’s not allowing me running it twice.