- Home /
GUI functions switching
Hello, I'm new to C# and Unity and to scripting in general. I've made a decent Main Menu that is able to bring the player to a tutorial world. Now in this tutorial world I want to call GUI functions to explain to the player how everything works. But how can I switch the text a GUI.Box is displaying?
Answer by Professor Snake · Jan 20, 2013 at 06:56 PM
You can use simple if statements with booleans. In order to show a group of GUI elements, you use something like the code below and modify the booleans to determine which message(s) to show:
var showMessage:boolean[];
function OnGUI(){
if(showMessage[0]){
//Any GUI you want to show here.
}
if(showMessage[1]){
//other GUI elements here.
}
//more checks here
}
Alternatively, you could have one group of GUI events with modifiable contents, and just modify whether the GUI is visible and the contents:
var showGUI:boolean=false;
var labelText:String="Hello, and welcome to my game";
var label2Text:String="This is a training level.";
function OnGUI(){
if(showGUI){
GUI.Label(Rect(0,0,100,100),labelText);
GUI.Label(Rect(0,100,100,100),label2Text);
}
}
Things is. I'm writing in C#. As far as I see this is Java. I do understand it a little bit, but not totally.
You can easily convert the code by changing "var name:Type" to "Type name" and "function name()" to "void name()". Besides, the code was there to give you an idea of how it works and encourage you to write your own version of it, not do the entire work for you.
I'm sorry, I've tried to understand the script but I simply can't. I tried: public bool TextBox1 = GUI.Box(new Recht(etc.), Text1); public string Text1 = "Welcome to ...."; But it just doesn't seem to work. I can't put it into another function.
$$anonymous$$y idea is to say: public void OnLevelWasLoaded(int level) { if(level = 1) Time.timeScale = 0; }
public void StartGame() { if(Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.Return)) Time.timeScale = 1; TextBox1 = false; }
There is no reason to Assign a GUI.Box to a boolean. You can keep your loading code and add void OnGUI(){ if(textbox1){ //GUI.Box call here}}
Answer by Dann858 · Jan 21, 2013 at 02:08 PM
I'm sorry, I've tried to understand the script but I simply can't. I tried: public bool TextBox1 = GUI.Box(new Recht(etc.), Text1); public string Text1 = "Welcome to ...."; But it just doesn't seem to work. I can't put it into another function.
My idea is to say:
public void OnLevelWasLoaded(int level) {
if(level = 1)
Time.timeScale = 0;
}
public void StartGame() {
if(Input.GetKeyUp(KeyCode.Return))
Time.timeScale = 1;
TextBox1 = false;
}
I know, But the add comment didn't allow me to put a code in my response. Like when you give an answer or ask a question.
It does allow you to put code in your comment, you just have to format it like in the answer field:
Add an empty line before and after the code block
indent every line within the code block by at least 4 spaces
That's all. Here i copied your code example:
public void OnLevelWasLoaded(int level) {
if(level = 1)
Time.timeScale = 0;
}
public void StartGame() {
if(Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.Return))
Time.timeScale = 1;
TextBox1 = false;
}
If you are too lazy to format it "manually", just write the comment in the answer field and when you're done, copy it into the comment field.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Making a Tutorial in th emiddle of screen and more efficiant 1 Answer
Multiple Cars not working 1 Answer
When I pause my game and enable canvas and then resume my keyboard starts controlling the menu... 1 Answer
Handling Undo/REdo in EditorWindow when editing DB record. 0 Answers