Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Can't break out of OnGUI()

Discussion in 'Scripting' started by cystemic, May 30, 2014.

  1. cystemic

    cystemic

    Joined:
    Mar 18, 2012
    Posts:
    7
    Hi everyone,
    I'm making a game where I want the player to press an object on screen which switches a boolean which triggers OnGUI() and when you have pressed a button from the scrollview then it should neatly disappear, however I've run in to a problem. When I set the bool Selecting to true, then the scrollview pops up with lots of buttons and no dramas but once I press one of the buttons when it should technically disappear, the GUI remains in place and the game keeps going in the background. Does anyone have any ideas how to get rid of OnGUI() once it's done it's thing? I've tried isolating the script in a different object and then destroying that object but it's the same, the GUI remains on screen no matter what I do :( I've tried using a selection grid but that was even worse... Any help would be super greatly appreciated. Thanks

    Code (csharp):
    1. void OnGUI(){
    2.                 if (Selecting==true) {
    3.                     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);
    4.                         for (int i=0; i<20; i++) {
    5.                                 //if(Selecting){
    6.                                         rect = new Rect(0,i*(Screen.height/4),Screen.width,Screen.height/4);
    7.                                         Tub = Resources.Load("Art/Tubs/"+Flavour[i], typeof(Texture)) as Texture;
    8.                                         Icon =Resources.Load("Art/Icons/"+Flavour[i], typeof(Texture)) as Texture;
    9.                                         GUI.DrawTexture(rect,Tub);
    10.                                         GUI.Label (rect,Icon);
    11.                                         if(GUI.Button (rect,Flavour[i],Tubs)){
    12.                                            
    13.                                             SelectedFlavour = Flavour[i];
    14.                                             print(SelectedFlavour);
    15.                                             FlavourSwitch();
    16.                                             Selecting = false;
    17.                                             print ("in for loop: "+Selecting);
    18.                                             break; 
    19.                                         //}
    20.                                 }
    21.                             }
    22.                            
    23.                         GUI.EndScrollView ();
    24.                     }
    25.  
    26.     }
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Code (csharp):
    1.  
    2. void OnGUI()
    3. {
    4.     if (!Selecting)
    5.         return;
    6.  
    7.     // rest of your code minus the if (Selecting) conditional
    8. }
    9.  
    The break is unnecessary.
     
  3. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    489
    Clicking it works fine for me. Hm
     
  4. cystemic

    cystemic

    Joined:
    Mar 18, 2012
    Posts:
    7

    I tried your code Kelso but it does the same as my code, you click it and the GUI pops up but when you press a button, it doesn't go away. I need it to stop displaying after one of the buttons is pressed. Any ideas? I've been wracking my brain for 2 days now...

    I made a video to illustrate the problem: http://youtu.be/ki1OFH4TRs4
     
    Last edited: May 31, 2014
  5. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    There's code snippet missing here... What put Selecting to true?
     
  6. cystemic

    cystemic

    Joined:
    Mar 18, 2012
    Posts:
    7
    Selecting happens in another function:

    public void ScoopSelected(string scoop){
    ScoopSelection = false;
    SelectedScoop = GameObject.Find (scoop);
    print (SelectedScoop.name);
    Selecting = true;

    }

    I've tested it seperately and it works fine. The console even prints "Selecting is false" when it is executed in the for loop...