- Home /
Hiding OnGUI?
Hi everybody,
Having a bit of an issue with OnGUI(). I have a boolean called Selecting which I would like to use to control when OnGUI() displays a scrollview onto my screen. So far it works ok. When I switch Selecting to true, it pops up with the arrays of buttons I want but when I click the button and change Selecting back to false, the scrollview remains, even though the functions that the button press calls successfully do their job. Is there a way to stop OnGUI() from doing this? Any help would be appreciated.
void OnGUI(){
if (Selecting) {
scrollPosition = GUI.BeginScrollView (new Rect(0,0,Screen.width+20,Screen.height),scrollPosition,new Rect(0,0,Screen.width,(Screen.height/6)*30-10),false,false);
for (int i=0; i<20; i++) {
if(Selecting){
rect = new Rect(0,i*(Screen.height/4),Screen.width,Screen.height/4);
Tub = Resources.Load("Art/Tubs/"+Flavour[i], typeof(Texture)) as Texture;
Icon =Resources.Load("Art/Icons/"+Flavour[i], typeof(Texture)) as Texture;
GUI.DrawTexture(rect,Tub);
GUI.Label (rect,Icon);
if(GUI.Button (rect,Flavour[i],Tubs)){
SelectedFlavour = Flavour[i];
print(SelectedFlavour);
FlavourSwitch();
Selecting = false;
break;
}
}
}
GUI.EndScrollView ();
}
}
Why do you have twice the Selecting check? I don't see why it would be the reason for your problem but I don't see why you have either.
All I can think of is that 'Selecting' isn't being changed. Try outputting its state to the log and see what happens.
as said above, try to remove the if(selecting) from the for loop and the break at the end then you have to do some debugging with your selecting flag... check when it change and if it actualy does...
Selecting is changing but the GUI still displays when I want it gone. For some reason if there isn't a second selecting check, it just loads up the GUI as soon as I enter the game :/
Answer by cystemic · May 30, 2014 at 02:48 AM
I've tried to regulate this by moving the GUI to an outside object which is created when the GUI is needed and destroyed when something in the gui is selected but I still get the same result. The GUI is still suspended on the screen after i press the button and the object is not destroyed. I have tried every kind of combination of Destroy but it just stays in the hierarchy. Please help :(
public class Test : MonoBehaviour {
ScoopAssembler assembler;
// Use this for initialization
void Start () {
assembler = GameObject.Find ("Generator 1").GetComponent<ScoopAssembler> ();
}
void OnGUI(){
//if (assembler.Selecting==true) {
assembler.scrollPosition = GUI.BeginScrollView (new Rect(0,0,Screen.width+20,Screen.height),assembler.scrollPosition,new Rect(0,0,Screen.width,(Screen.height/6)*30-10),false,false);
for (int i=0; i<20; i++) {
//if(Selecting){
assembler.rect = new Rect(0,i*(Screen.height/4),Screen.width,Screen.height/4);
assembler.Tub = Resources.Load("Art/Tubs/"+assembler.Flavour[i], typeof(Texture)) as Texture;
assembler.Icon =Resources.Load("Art/Icons/"+assembler.Flavour[i], typeof(Texture)) as Texture;
GUI.DrawTexture(assembler.rect,assembler.Tub);
GUI.Label (assembler.rect,assembler.Icon);
if(GUI.Button (assembler.rect,assembler.Flavour[i],assembler.Tubs)){
assembler.SelectedFlavour = assembler.Flavour[i];
print(assembler.SelectedFlavour);
assembler.FlavourSwitch();
assembler.Selecting = false;
print ("in for loop: "+assembler.Selecting);
Destroy(this.gameObject);
//}
}
}
GUI.EndScrollView ();
//}
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
[Solved] GUI Error? 1 Answer
How to be specific and universal with your onGUI controls? 1 Answer