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.
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.
UnityGUI controls make use of a
special function called `OnGUI()`. The
`OnGUI()` function gets called every
frame as long as the containing script
is enabled - just like the `Update()`
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.