- Home /
GUI, making main page lead into an instructions page
I am following will goldstones Unity 3.x book and my script doesn't seem to work, but I get no errors.
Here's the code
else if (menuPage == "instructions")
{
Debug.Log ("does it work?");
GUI.Label(new Rect(instructions),
"You awake on a mysterious island... Find a way to signal for help or face certain doom!");
Debug.Log("doesitwork2?");
if(GUI.Button(new Rect(quitButton), "Back"))
{
Debug.Log ("doesitwork3");
audio.PlayOneShot(beep);
menuPage="main";
}
}
The first two debug.logs work perfectly, but the third doesn't run at all. Also, all of the text in the label doesn't seem to show up at all, but no errors. I don't think that "if(GUI.Button(new Rect(quitButton), "Back"))" runs at all. I keep looking over the book but I don't see any differences between what I have and what the book has.
Anyone have an Idea?
Here is the full code just in case.
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (AudioSource))]
public class $$anonymous$$ain$$anonymous$$enuGUI : $$anonymous$$onoBehaviour
{
public AudioClip beep;
public GUISkin menuSkin;
public Rect menuArea;
public Rect playButton;
public Rect quitButton;
public Rect instructionsButton;
public Rect menuAreaNormalized;
public string menuPage = "main";
public Rect instructions;
void Start()
{
//following 3 lines are used to make the game work on all resolutions
menuAreaNormalized = new Rect(menuArea.x * Screen.width - (menuArea.width * .5f),
menuArea.y * Screen.height - (menuArea.height *.5f),
menuArea.width, menuArea.height);
}
void OnGUI()
{
// makes all gui elements have this skin. $$anonymous$$akes it easier to edit all elements in one go
GUI.skin = menuSkin;
GUI.BeginGroup (menuAreaNormalized);
//making button 1... we access GUI class, it's button component, pass in a rect value, and then assign it stuff.
if(menuPage == "main")
{
if(GUI.Button (new Rect(playButton), "Play"))
{
//we send "island" to our buttonaction function, to load the island level
StartCoroutine("ButtonAction", "Island");
}
//button 2...
if(GUI.Button (new Rect(instructionsButton), "Instructions"))
{
audio.PlayOneShot(beep);
//doesn't enable play or quit buttons, sets page to instructions page
menuPage = "instructions";
}
//button 3...
if(GUI.Button (new Rect(quitButton), "Quit"))
{
//we send "quit" to our buttonaction function...
StartCoroutine("ButtonAction", "quit");
}
else if (menuPage == "instructions")
{
Debug.Log ("does it work?");
GUI.Label(new Rect(instructions),
"You awake on a mysterious island... Find a way to signal for help or face certain doom!");
Debug.Log("doesitwork2?");
if(GUI.Button(new Rect(quitButton), "Back"))
{
Debug.Log ("doesitwork3");
audio.PlayOneShot(beep);
menuPage="main";
}
}
}
GUI.EndGroup ();
}
IEnumerator ButtonAction(string levelName)
{
audio.PlayOneShot(beep);
yield return new WaitForSeconds(.35f);
if(levelName != "quit")
{
Application.LoadLevel(levelName);
}
else
{
Application.Quit();
Debug.Log ("Have Quit");
}
}
// Update is called once per frame
void Update () {
}
}
generally, beginners should not use else-if it's just too easy to screwup.
t's just too confusing. i have a lifetime of experience in softwar and I can't understand your code , at all.
re-write using simple "breakaway" code. it's a technique you need to master and extremely simple.
so it's something like this ..
if ( quit ..)
{
do that stuff
do that stuff
return;
}
if ( button down ..)
{
do that stuff
do that stuff
return;
}
if ( button up ..)
{
do that stuff
do that stuff
return;
}
if ( instructions ..)
{
do that stuff
do that stuff
return;
}
if ( quit button pressed ..)
{
do that stuff
do that stuff
return;
}
notice each block ends with "return;" You get the idea right?
So re-write everything that way.
No program$$anonymous$$ technique is perfect in all situations, but this is what you need here and now.
Secondly. you see you have a huge amount of code under if(menuPage == "main")
You must change it like this
if (menuPage == "main")
{
_hereIHandleA$$anonymous$$ain();
}
function _hereIHandleA$$anonymous$$ain()
{
lots of code here
}
A person just can't exist with untidy code. You must tidy up first eh !
It's like having a dirty, unkept bed. If you can't keep the sheets on your bed spotless, instantly kill yourself to make more room on the planet for other people :)
Similarly you must tidy-up your code ok?
try writing "breakaway" code like above as that will probably help dramatically in the first instance.
don't use "else-if" if you're a beginner. it's just too easy to get confused.
Thank you for the response.
I understood my code up until he add us add the else if, so thank you for the alternative. I would have approached this differently normally, but I was trying to follow the books instructions. No offense to the books author though.. because I am pretty sure I did something wrong somewhere to make it look like that.
Anyways, I will try writing the code in the way that you've recommended. Thanks.
Answer by SubatomicHero · May 10, 2013 at 07:09 AM
else if (menuPage == "instructions")
I can see from your code its linking to :
if(GUI.Button (new Rect(quitButton), "Quit"))
whereas it needs to link to :
if(menuPage == "main")
Unless I have misunderstood what you are saying I don't think that is correct. I want the instruction page which is the purpose of the else if, to create a new label giving me instructions, and then create a back button in place of my quit button to take me back to the main instruction page when the player clicks it.
After relooking at my code I realized that my GUI.begingroup and end group were not lined up. Now all three debugs run but I get the error : "GUI Error: You are pushing more GUIClips than you are popping. $$anonymous$$ake sure they are balanced)"
Did I mess it up further?
Answer by Bleakraven · Dec 03, 2013 at 02:43 PM
Hi! I had the same problem as you, exactly the same code. Our problem is that our "else if" is inside the initial if. It has to be outside the curly braces from our first if. Hope this helps!
:)
Your answer
Follow this Question
Related Questions
GUI.Label positioning for many device resolutions 1 Answer
GuiMatrix ? 1 Answer
Creating a component from a button click 1 Answer
GUI.Label appearing behind GUI.Button 1 Answer
when button pressed, textfield appears 3 Answers