Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 czehner1 · Mar 20, 2017 at 01:27 AM · c#uibuttonmenusetactive

Script Two Button Functions At Once

Hi all!

I am having some difficulty getting a cyclical (as in works for two buttons that toggle the other) button toggle to work. This bit of code will successfully find both objects whether or not they are active (the Debug always returns the correct status of the corresponding UI elements,) and can go from MainMenu -> OptionMenu, but will not work the opposite way around. I checked to be sure that everything was linked properly in the editor and each UI element is virtually the same as the other.

     public GameObject MainPanel;
     public GameObject OptionPanel;
 
     void Start()
     {
         // Find inactive GameObjects by finding their parent then their child
         MainPanel = GameObject.Find ("Canvas").transform.Find ("MainMenuPanel").gameObject;
         OptionPanel = GameObject.Find ("Canvas").transform.Find ("OptionsPanel").gameObject;
 
         string MainStatus = MainMenuUp ().ToString ();
         string OptionStatus = OptionMenuUp ().ToString ();
 
         Debug.Log ("Option Menu is: " + OptionStatus);
         Debug.Log ("Main Menu is: " + MainStatus);
     }
 
     public void OnClickToggle()
     {
         if (OptionMenuUp () == true) {
             MainPanel.SetActive (true);
             OptionPanel.SetActive (false);
         }
 
         if (MainMenuUp () == true) {
             MainPanel.SetActive (false);
             OptionPanel.SetActive (true);
         }
     }
 
     bool MainMenuUp ()
     {
         return MainPanel.activeInHierarchy;
     }
 
     bool OptionMenuUp ()
     {
         return OptionPanel.activeInHierarchy;
     }



Is it impossible to link this script to two different buttons to have them activate/inactivate each other at the same time? Or is there something I missed with my code? Any info will help

Comment
Add comment · Show 1
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 rhylvin2016 · Mar 20, 2017 at 02:27 AM 0
Share

so you're attaching this script to only 1 object right?

1 Reply

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

Answer by aFeesh · Mar 20, 2017 at 02:44 AM

Try using an else-if like so instead of two separate if's.

          if (OptionMenuUp () == true) {
              MainPanel.SetActive (true);
              OptionPanel.SetActive (false);
          } else  if (MainMenuUp () == true) {
              MainPanel.SetActive (false);
              OptionPanel.SetActive (true);
          }
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 RobAnthem · Mar 20, 2017 at 02:54 AM 0
Share

Yeah essentially what the two If statements were doing was this.

First statement, checks if the options menu is active, then it sets the main menu to active if it is.

Second statement checks if the mainmenu is active, and if it is, it sets options menu to active.

Both are being run every time, consecutively. In other words, the ONLY result was going to be the Options $$anonymous$$enu being active.

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

8 People are following this question.

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

Related Questions

Change the position of a UI component through pressing a UI Button 1 Answer

I need to move a sprite when an UI button is clicked 1 Answer

Change scene and play it by clicking UI Button? Scene does just load but not start playmode? 1 Answer

UI works in editor, but not on mobile device 1 Answer

I made an resume button, but I don't know how to code it to close my pause menu, any tips? 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