• 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 Lesrd · May 28, 2012 at 05:25 PM · clickunresponsive

Unresponsive GUIToggle - Conceptual Problem?

Before I begin, my apologies if this question has been answered before: I've searched around but unfortunately could not find an answer.

I know the basics of GUIToggle, and I know to use it with a Button style in order to create a GUIToggle disguised as an "On/Off" button.

I'm working on a project with a few other people and in this project I made a main menu screen with selectable entries using several GUIToggle calls. When the project is run, if the user starts the game (detected through GUIToggle), another scene is loaded (the GameObject that has the menu screen MonoBehavior and screen management is set to not be destroyed between scenes) and the player gets in-game.

Now, I implemented a pause menu using the same concept that was used in the main menu: GUIToggle calls for the various entries of the pause menu. The same button style of the main menu is used in this one. When I hover the mouse over the entries, the toggles correctly use the OnHover texture I assigned in the GUISkin custom styles I created (used in the main menu as well), so everything seems to work fine. However, when I try to click on one of the GUIToggle, it simply does not register the click!

I'm doing the GUIToggle calls inside OnGUI(), and I've tested mouse clicks from the Input class, and the clicks are being registered there.

Before I try to use my hammer to get this to work (thought about manually checking Rect coordinates when a mouse click occurs), I wanted to know if (I'm quite sure it's something right under my nose) there might be any sort of MonoBehavior (that I'm unaware of) in the project that may be inhibiting GUIToggle from returning true on this pause menu... Something that conflicts with my (supposedly) correct use of GUIToggle.

Thanks for your time.

EDIT: here's the relevant conde at the moment:

 void OnGUI()
     {
         if (IsActive)
         {
             GUI.skin = _screenManager.MenuSkins;
             _sizer.BeginGUI();
 //            Debug.Log(_screenManager.CurrentState.ToString());
             if (_screenManager.CurrentState.Equals(ScreenManager.ScreenState.Paused))
             {
                 GUI.DrawTexture(_menuBox, _menuBoxBackground, ScaleMode.StretchToFill, true);
                 GUI.Label(_titleRect, "Game Paused", _titleStyle);
             //    Debug.Log(_screenManager.CurrentState);
                 if ((bool)(_buttonStates[(int)ButtonID.ResumeGame] = GUI.Toggle(
                     (Rect)_buttonRects[(int)ButtonID.ResumeGame], (bool)_buttonStates[(int)ButtonID.ResumeGame],
                     (string)_buttonTexts[(int)ButtonID.ResumeGame], _buttonStyle)))
                 {
                     Debug.Log("Resume Game pressed");
                     _buttonStates[(int)ButtonID.ResumeGame] = false;
                     UnPause();
                 }
                 else if ((bool)(_buttonStates[(int)ButtonID.Options] = GUI.Toggle(
                     (Rect)_buttonRects[(int)ButtonID.Options], (bool)_buttonStates[(int)ButtonID.Options],
                     (string)_buttonTexts[(int)ButtonID.Options], _buttonStyle)))
                 {
                     Debug.Log("Options pressed");
                     _buttonStates[(int)ButtonID.Options] = false;
 //                    _screenManager.CurrentState = ScreenManager.ScreenState.Options;
                     //TODO create and configure an Options screen
                 }
                 else if ((bool)(_buttonStates[(int)ButtonID.ReturnToMenu] = GUI.Toggle(
                     (Rect)_buttonRects[(int)ButtonID.ReturnToMenu], (bool)_buttonStates[(int)ButtonID.ReturnToMenu],
                     (string)_buttonTexts[(int)ButtonID.ReturnToMenu], _buttonStyle)))
                 {
                     _screenManager.CurrentState = ScreenManager.ScreenState.MainMenu;
                     _buttonStates[(int)ButtonID.ReturnToMenu] = false;
                     Debug.Log("Return to menu pressed");
                 }
             }
             _sizer.EndGUI();
         }
     }

 private void UnPause()
 {
     Debug.Log("Unpaused!");
     _screenManager.CurrentState = ScreenManager.ScreenState.InGame;
     Time.timeScale = _defaultTimeScale;
     _defaultFixedTimeDelta = Time.fixedDeltaTime;
     Object[] objects = FindObjectsOfType(typeof(GameObject));
     Debug.Log(objects.Length);
     foreach (GameObject go in objects)
     {
         if (go != this)
             go.SendMessage("OnResumeGame", SendMessageOptions.DontRequireReceiver);
     }
 }
 // Update is called once per frame
 void Update()
 {
     if (IsActive)
     {
         if (_screenManager.CurrentState.Equals(ScreenManager.ScreenState.InGame))
         {
             if (Input.GetMouseButtonDown(0))
             {
                 Debug.Log("Mouse click detected");
                 // do something
             }
             if (Input.GetKey(KeyCode.Escape))
             {
                 _screenManager.CurrentState = ScreenManager.ScreenState.Paused;
                 Time.timeScale = 0;
                 _defaultFixedTimeDelta -= _defaultTimeScale;
                 Debug.Log("Game paused!");
                 Object[] objects = FindObjectsOfType(typeof(GameObject));
                 Debug.Log(objects.Length);
                 
                 foreach (GameObject go in objects)
                 {
                     if(go != this)
                         go.SendMessage("OnPauseGame", SendMessageOptions.DontRequireReceiver);
             //        Debug.Log(go.ToString());
                 }
             }
         }
         else if (_screenManager.CurrentState.Equals(ScreenManager.ScreenState.Paused))
         {
             if (Input.GetMouseButtonDown(0))
             {
                 Debug.Log("Mouse click detected");
                 // do something
             }
             if (Input.GetKey(KeyCode.Escape))
             {
                 UnPause();
             }
         }
     }
 }
Comment
Add comment · Show 8
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 whydoidoit · May 28, 2012 at 05:33 PM 0
Share

Could you post the code, by editing the question? Would be good to see it.

avatar image Lesrd · May 28, 2012 at 05:36 PM 0
Share

$$anonymous$$ind of hard as it uses functionality from different parts of the project... I'll post the main OnGUI function of the pause menu for now.

avatar image whydoidoit · May 28, 2012 at 05:38 PM 0
Share

Great - It doesn't sound like you have a conceptual problem to me. But my brain is a little addled today ;)

avatar image Lesrd · May 28, 2012 at 05:39 PM 0
Share

Added (what I believe to be) the relevant code by editing the original post!

EDIT: Pausing/Unpausing through ESC key works fine, and the pause menu is drawn properly and the GUIToggle is correctly using the style's OnHover: when I move the mouse cursor over the entry, it changes to the Hover texture I set... It's just that when I click on any of those GUIToggle it should return true and fall into one of those cases but it's not happening for some reason =/

avatar image whydoidoit · May 28, 2012 at 05:43 PM 0
Share

Nothing in the console log? No invalid casts or anything?

Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by whydoidoit · May 28, 2012 at 05:47 PM

Ok so you are immediately turning off any button you turn on!

For example:

   if ((bool)(_buttonStates[(int)ButtonID.ResumeGame] = GUI.Toggle(
               (Rect)_buttonRects[(int)ButtonID.ResumeGame], (bool)_buttonStates[(int)ButtonID.ResumeGame],
               (string)_buttonTexts[(int)ButtonID.ResumeGame], _buttonStyle)))
           {
               Debug.Log("Resume Game pressed");
               _buttonStates[(int)ButtonID.ResumeGame] = false;
               UnPause();
           }

If the GUI toggle becomes true the body of the if is executed - which immediately makes it false again.

The state of the button is being returned from the GUI.Toggle (not whether it has been pressed) so your if part looks right, but then you go ahead and set it false.

Comment
Add comment · Show 22 · 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 Lesrd · May 28, 2012 at 05:52 PM 0
Share

But does that constitute a problem necessarily? Because this is what I'm doing on my main menu and it works fine...

Besides, what's the harm in setting it back to false immediately? I mean, this is a simple state machine, and if it falls into that if's body, it will also call UnPause() which sets the current state to InGame, which would forbid the code flow from drawing these GUIToggles in the near future: I'm only calling GUIToggle inside the check for state==Paused, which should become false as soon as I run the GUIToggle's body, which sets state==InGame, so I don't understand how that would be the problem...

In theory what this would do (for the case of the first GUIToggle()) is to close the pause menu and resume gameplay!

Thanks for the help so far.

avatar image whydoidoit · May 28, 2012 at 05:55 PM 0
Share

Hey no problem - why would you be using a Toggle if what you want is a button though? Because next time through this loop it is going to be false again and turned off.

If you want something to press, use a GUI.Button - it returns true when it is pressed. I was presu$$anonymous$$g that you were using the state of that Toggle as you are storing it...

avatar image whydoidoit · May 28, 2012 at 05:55 PM 0
Share

But currently it will never be true after OnGUI

avatar image Lesrd · May 28, 2012 at 06:01 PM 0
Share

If I just use a regular button, I cannot make it have a given animation when the mouse hovers over it (at least not in any way that I know of that is trivial), as it currently is... I saw several topics (perhaps not only on this online system) where people wanting animated buttons were told to use GUIToggle with a button's style!

Considering this necessity, I'm merely attempting to use this aesthetic property of GUIToggle and discarding its state: because whenever there is a GUIToggle state change, I'm either going into another menu or back in-game, and that would be coordinated through the state-machine I made.

Hope I didn't confuse you!

avatar image whydoidoit · May 28, 2012 at 06:02 PM 0
Share

You are right though, that is strange that you never get it become true - more whirring noises co$$anonymous$$g from my head

Show more comments
avatar image
0

Answer by Lesrd · May 28, 2012 at 09:07 PM

I tested on both GUIToggle and on the GUIButton calls:

This is what I'm getting (there was also a lot of Layout/Repaint spam):

  • mouseDown;

  • mouseUp;

  • mouseDrag; (sometimes)

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
avatar image
0

Answer by Lesrd · May 28, 2012 at 08:40 PM

I'm to try that one soon.

I've found something meanwhile: if I do Screen.lockCursor = false; at the top og OnGUI(), then at least it enters the "Options" GUIToggle. I'm going to try to disable script that locks the cursor and check if that works. If not, I'll try your idea right away!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Quad Objects not responding to clicks. 1 Answer

DontDestory gameobject doesn't work on button click after a new scene loads 2 Answers

how do i fix not being able to click properly in my clicker game? 1 Answer

Weird Screen when launching game for windows 0 Answers

Pass Clicks Through Camera 2 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