Need a help with a script

Hello,I am making a weapon shop in my game and I would really appreciate if someone could help me with this script.

 if (GUI.Button (Rect (500,250,90,90), "AK-47")) {   //ak je tlacidlo stlacene + peniaze
          
 
 
 if (Peniaze.peniaze >= 15000)    { //ak ma hrac dostatok penazi         
          
          
          
          
          Peniaze.peniaze = Peniaze.peniaze - 15000;
          
          //pridat ak -  47 do inventara 
          kupit = false;
          Screen.showCursor = false;



}        
 
 else 
 
 {
 
 
 GUI.Box (Rect (450,150,500,500), "NEDOSTATOK PENAZI");





}

}

}

}

}

So,the if button is pressed and variable peniaze is bigger or equals 15000,15 000 will be deducted from it and weapon will become active.This part of the script is fine.But i need help with the else statement. It should work this way : if variable peniaze is not equal or bigger than 15000 , the text NEDOSTATOK PENAZI will display at the screen,but nothing happens, it looks like the game is completely ignoring the else statement .Could somebody help me with this and explain where is the mistake?

P.S. I am from Slovakia and my English is not so great so i am sorry for any mistakes and i hope you can understand me :smiley: Thanks.

Hi, your “else” will only be executed when the button is clicked, that is: only 1 time = one call = during one frame of the game.

Then, when you ask Unity to display a GUI.box, it will display it only during one frame (a Debug.Log() can confirm that); That’s why you can’t see it.

Maybe you want to display a modal window to warn the user using GUI.ModalWindow.

According to your code, the lines are executed on the button press. However, GUI.Button works on mouse release or finger release. So, this code is executed in only one frame which is normally 1/60 th of a second. Also you have written GUI.Box inside the GUI.Button. So GUI.Box will be drawn only in on frame which is nearly visible. However, the else part must be executing fine. So, the possible solution to your problem is:

bool ShowMessage = false;

void OnGUI()
{
    if (GUI.Button (Rect (500,250,90,90), "AK-47"))
         {   //ak je tlacidlo stlacene + peniaze    
  
  
              if (Peniaze.peniaze >= 15000)    
               { //ak ma hrac dostatok penazi   
                    Peniaze.peniaze = Peniaze.peniaze - 15000;
           
                   //pridat ak -  47 do inventara 
                    kupit = false;
                    Screen.showCursor = false;
                }        
       
                else 
               { 
                   ShowMessage = true;
               }
         }
       if( ShowMessage)
       {
             GUI.Box (Rect (450,150,500,500), "NEDOSTATOK PENAZI");
       }
 }
 
 }