How to create a Pop-up window??

Hello,

I would like to ask how to create a Pop-up window. For example, currently i am working on a puzzle games. Which mean that player click on "A B E" (invalid word form), it will have a pop up window indicate that "Wrong word formation, please try again". Please Help.

Thank you

make a boolean in your code which gets to true when this condition happens

then inside OnGUI() function put whatever gui control you wish to show inside an if statement which checks this boolean

1 Like

hello amirghayes,

Thank you for the reply. but i do not know how to get started with it. Could you give some example on how i can do it? I mean the onGUI.

function onGUI()
{
//pop up window
}

Search the scripting reference for GUI.Window or GUILayout.Window.

bool openWindow;

function OnGUI() {
if(openWindow){
//Make your GUI items here. Set openWindow to true when you want the pop up to appear, and false when you want it gone.
}
}

lol here you got the answers very fast before I even try
you guys rocks

hello all, thank you for the reply :)) I will try it out.. :)

hi,

I got some questions. I do not know where should i place my function onGUI()??

Basically what i want is for example, player click " A T K " -> which is invalid word, i want to pop up a window saying "Sorry wrong word formation, please try again". I have a script called mouseclick.js that is place on every alpahebets. ( "A", "T", "K"). I have another script called gamecontrol which will check whether A T K is a valid words. So my qn is i should put the function onGUI at the mouse click or the gamecontrol?? I put it in gamecontrol, but it doesnt work properly.

first I think it is easier for us if you show us the code so that we know where is the error
if I understand right then you did it right as the OnGUI() should be placed in the gamecontrol script , tho you can put it in whatever script but you have to be able to set its bolean to true from the gamecontrol script - neglect the last part if it is hard for you
anyway I will try to put a code snippet here to try and see if it works
in C# tho

put the construction in the gamecontrol script

private bool showPopUp = false;

void Update()
{
 // put your if statement here and set showPopUp to true if the condition is true 
 // put an else statement to set the bolean to false if it is not true

}

void OnGUI()
{
  if (showPopUp)
   {
     GUI.Window(0, new Rect((Screen.width/2)-150, (Screen.height/2)-75
            , 300, 250), ShowGUI, "Invalid word");

   }
}

void ShowGUI(int windowID)
     {
         // You may put a label to show a message to the player

         GUI.Label(new Rect(65, 40, 200, 30), "PUT YOUR MESSAGE HERE");

         // You may put a button to close the pop up too 

          if (GUI.Button(new Rect(50, 150, 75, 30), "OK"))
         {
             showPopUp = false;
             // you may put other code to run according to your game too
         }

      }

of course you can change the numbers in this example to whatever suits your game

I hope that heleped

if you can't understnd this C# code or can't convert it to JS , then let me know and I will try to make the JS version for you

good luck

[quote=“amirghayes”, post:9, topic: 449912]
first I think it is easier for us if you show us the code so that we know where is the error
if I understand right then you did it right as the OnGUI() should be placed in the gamecontrol script , tho you can put it in whatever script but you have to be able to set its bolean to true from the gamecontrol script - neglect the last part if it is hard for you
anyway I will try to put a code snippet here to try and see if it works
in C# tho

put the construction in the gamecontrol script

private bool showPopUp = false;

void Update()
{
 // put your if statement here and set showPopUp to true if the condition is true 
 // put an else statement to set the bolean to false if it is not true

}

void OnGUI()
{
  if (showPopUp)
   {
     GUI.Window(0, new Rect((Screen.width/2)-150, (Screen.height/2)-75
            , 300, 250), ShowGUI, "Invalid word");

   }
}

void ShowGUI(int windowID)
     {
         // You may put a label to show a message to the player

         GUI.Label(new Rect(65, 40, 200, 30), "PUT YOUR MESSAGE HERE");
        
         // You may put a button to close the pop up too 

          if (GUI.Button(new Rect(50, 150, 75, 30), "OK"))
         {
             showPopUp = false;
             // you may put other code to run according to your game too
         }

      }

of course you can change the numbers in this example to whatever suits your game

I hope that heleped

if you can’t understnd this C# code or can’t convert it to JS , then let me know and I will try to make the JS version for you

good luck
[/quote]

hello amirghayes, thank you so much for the help… :slight_smile: I will try to convert it back to javascript. Thank you for follow up with this thread. I nearly going to open new thread for answer :stuck_out_tongue: Anyway to add on, i don’t know if i did mention about it, i will have two pop up window, one will be for the correct word form one for the incorrect word formed. So am i right to say that i will need something like two showGUI or any boolean variables?

You are welcome
if you gonna need 2 windows to be shown at the same time , then yes I think you gonna need 2 bolean variables
but I don't think that is your case
if you gonna need 1 window to pop up at the time , you then keep the window but change the condition to alter the label or window message value
like u change the part of "Invalid word" with a string variable , then set the value of this string to "Invalid word" or "Correct word" in the condition check function or code , this way you don't need to type a code for 2 different windows for different condition

and you can do the same for label in "PUT YOUR MESSAGE HERE"

hope that answered your question
good luck

function onGUI()
{
    if(isCorrect)
    {
        GUI.Window(0, new Rect((Screen.width/2)-150, (Screen.height/2)-75, 300, 250), showGUI, "Congratulation!!");
    }
    else
    {
        GUI.Window(0, new Rect((Screen.width/2)-150, (Screen.height/2)-75, 300, 250), showGUI, "Invalid Word Formed");
    }
}

function showGUI()
{
        if(isCorrect)
        {
            // You may put a label to show a message to the player
            GUI.Label(new Rect(65, 40, 200, 30), "You have formed the correct words!!! :)");
        }
        else
        {
            GUI.Label(new Rect(65, 40, 200, 30), "Sorry!! Please try again!!");
        }

        // You may put a button to close the pop up too 
        if (GUI.Button(new Rect(50, 150, 75, 30), "OK"))
        {
             isCorrect = false;
             // you may put other code to run according to your game too
         }
}

Hi, i try doing it. But nothing happened for this code. What wrong??

first of all I think this JS ? if so I don't think you need to put the word "new" before the "rect" I think you just type rect
I'm very weak in JS so forgive me ,
second the name of the function is case sensitive , it is OnGUI() , not onGUI()
here is my try with the code after adjusting it to your need
but again it is C# , if you realy need its JS version then I think other members could help you or give me a bit of time to figure it out for you

    private string windowMessage;
    private string labelMessge;

    void OnGUI()
    {
        if (isCorrect)
        {
            GUI.Window(0, new Rect((Screen.width / 2) - 150, (Screen.height / 2) - 75, 300, 250), showGUI, windowMessage);
        }
    }

    void showGUI()
    {
        if (isCorrect)
        {
            // You may put a label to show a message to the player
            GUI.Label(new Rect(65, 40, 200, 30), labelMessge);
        }

        // You may put a button to close the pop up too 
        if (GUI.Button(new Rect(50, 150, 75, 30), "OK"))
        {
            isCorrect = false;
            // you may put other code to run according to your game too
        }
    }

To make this work for you , you need to do the following

in the condition code that you check for validating
if he picked up the right compination then set the following variables

isCorrect = true ;
windowMessage = "Congratulation!!";
labelMessage = "You have formed the correct words!!! :)" ;

if he picked up wrong compination then set the following variables

isCorrect = true ;
windowMessage = "Invalid Word Formed";
labelMessage = "Sorry!! Please try again!!";

of course you can get the same goal with a lot of different ways , thats how programming is , and thats how I thought about it atm , may be I can do it in a different way in another time

at last I hope that helped and forgive me if something is wrong I'm still a noob
will try to help you though if it doesn't work like you expected

good luck

[quote=“amirghayes”, post:13, topic: 449912]
first of all I think this JS ? if so I don’t think you need to put the word “new” before the “rect” I think you just type rect
I’m very weak in JS so forgive me ,
second the name of the function is case sensitive , it is OnGUI() , not onGUI()
here is my try with the code after adjusting it to your need
but again it is C# , if you realy need its JS version then I think other members could help you or give me a bit of time to figure it out for you

    private string windowMessage;
    private string labelMessge;

    void OnGUI()
    {
        if (isCorrect)
        {
            GUI.Window(0, new Rect((Screen.width / 2) - 150, (Screen.height / 2) - 75, 300, 250), showGUI, windowMessage);
        }
    }

    void showGUI()
    {
        if (isCorrect)
        {
            // You may put a label to show a message to the player
            GUI.Label(new Rect(65, 40, 200, 30), labelMessge);
        }

        // You may put a button to close the pop up too 
        if (GUI.Button(new Rect(50, 150, 75, 30), "OK"))
        {
            isCorrect = false;
            // you may put other code to run according to your game too
        }
    }

To make this work for you , you need to do the following

in the condition code that you check for validating
if he picked up the right compination then set the following variables

isCorrect = true ;
windowMessage = “Congratulation!!”;
labelMessage = “You have formed the correct words!!! :)” ;

if he picked up wrong compination then set the following variables

isCorrect = true ;
windowMessage = “Invalid Word Formed”;
labelMessage = “Sorry!! Please try again!!”;

of course you can get the same goal with a lot of different ways , thats how programming is , and thats how I thought about it atm , may be I can do it in a different way in another time

at last I hope that helped and forgive me if something is wrong I’m still a noob
will try to help you though if it doesn’t work like you expected

good luck
[/quote]

HELLO amirghayes, thank you very much. Now it work properly I think is because of the case sensitive OnGUI() instead of onGUI that wise it is not working properly. You really help me alot :slight_smile: No worry.

Are you working on a game now as well? Maybe we can talk/discuss more. I need ideas for my games!!! HAHAH… :slight_smile:

hello,

just a random question, do you think that if the player form correct/valid words, i should pop up window? Or just pop up window if they form wrong word?? And something i am concern, it is very irritating to pop up window while playing game? In a dilemma. Because if i don't pop up window saying wrong words, the player wont know?? Need some comment.