- Home /
Situational GUI popup question.
I'm having trouble with a situational GUI popup. For example, when the game loads for the first time, I want to have a GUI pop up that starts the story out. You know, tells you what's going on... story stuff. I've gotten the popup to work how I want, but I need it to only pop up the first time the level loads, not when I go back to it later or anything like that.
I tried a simple script starting out with something like
if(Application.LoadLevel("Level 1"))
{
//GUI Button script here
}
But that just gives me errors.
I've also tried something like
function OnTriggerEnter (other : Collider) { if(other.tag=="Player"){ //GUI Script Here } }
function OnTriggerExit (other : Collider) { if(other.tag=="Player"){ Destroy(this); } }
But again, I just get errors. The second seems like the more likely aproach, I just don't know what to do for the script.
Answer by Lipis · Mar 17, 2010 at 12:53 PM
You are probably getting these errors because you are trying to create GUI
Controls not inside the OnGUI()
function.
From GUI Basics:
UnityGUI controls make use of a special function called
OnGUI()
. TheOnGUI()
function gets called every frame as long as the containing script is enabled - just like theUpdate()
function.
If you want to show something in the beginning of each level you can raise a flag whenever this script is started and if the flag is up display the start up message. After a while (or by clicking a button) the flag will be set to false and this message won't be displayed anymore, since we are not going to set the flag to true.
Attach this script to an object, to see an example:
var skin: GUISkin; var showIntro: boolean;
function Start() { showIntro = true; }
function OnGUI() { if (showIntro) { GUI.skin = skin; GUILayout.BeginArea(Rect(128, 128, 256, 256)); GUILayout.Box("Welcome"); GUILayout.Label("Hello, world!"); if (GUILayout.Button("Ok ok.. let me play!")) { showIntro = false; } GUILayout.EndArea(); } }
You can also create a new GUI Skin
(Assets->Create->GUI Skin
) and attach it to this script, in order to change the feel & like of your GUI
.
If you want to show a message whenever you are clicking on an GameObject
, then try doing a similar trick, but this time by enabling your flag in OnMouseDown()
function.
Cool, thanks. Now lets take the second case farther. Lets say for example I want a GUIbox with text to pop up when a player enters a certain area, or even better when they click on a specific object (like a note on a table) and I want it to pop up the GUI box. How do I go about doing that? I think I'm on the right track, but i'm not sure.
Or is there a script function that I can use that will call on another script (like a GUI script) and activate it when a certain action has been preformed?
I updated my answer.. I think I know why were getting these errors.. as for how to show some GUI controls from another script.. I think you have to ask another questions.. it's not a forum like site, so one question per question is better :)
Answer by lowbloodsugar · Mar 17, 2010 at 06:51 AM
Try moving the intro video into its own level. When you build the game add that level as level 0. And posting the game as level 1.
At the end of the intro video on level 0, load level 1. Then when you want to restart the game, load level 1.
Right, I got that to work, the only issue is i'm making a system that requires you to reload past levels (Like loading from a cave to the world in Oblivion) and so I don't want this to pop up when I reload the world again from somewhere totally unrelated to the begining scene. $$anonymous$$ake sense?
That's why I think If i could get the second script to work it'd work better, i could spawn the guy in the trigger, and then when he leaves it, it'll just destroy it.
Answer by Extrakun · Mar 17, 2010 at 09:45 AM
You need something that is called a finite state machine.
The simple way to do it is to have a variable to holds the state
var GUIState:int = 0;
function OnGUI() { switch (GUIState) { // game begins case 0: // ...GUI Script for game begins... break;
// tutorial
case 1:
//....
break;
} }
This, of course, will get tiring over time. So what you can do is to declare a class, called GUIState, has a base function called Display. Have new states to derive from the base class, overrides its update and add in the GUI code to display what you want there. But for a simple game, a simple switch-case would do.
Your answer
Follow this Question
Related Questions
Setting Scroll View Width GUILayout 1 Answer
How to change a GUI button texture through scripting 1 Answer
How can I set up a series of OnGUI Toggle buttons in a for loop? 1 Answer
GUI button off centre 1 Answer
GUI problem 1 Answer