Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by CW93 · May 10, 2013 at 07:00 AM · guibuttonlabelno errorsinstructions

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?

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CW93 · May 10, 2013 at 07:00 AM 0
Share

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 () {
     
     }
 }
 
 
avatar image Fattie · May 10, 2013 at 09:15 AM 0
Share

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.

avatar image CW93 · May 10, 2013 at 07:55 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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")
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CW93 · May 10, 2013 at 07:28 AM 0
Share

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?

avatar image
0

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!

:)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges