Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
1
Question by Jeoyinc · Sep 18, 2017 at 09:07 AM · buttononclick

How to Show and Hide UI panel interchangeably with button onclick in unity 2017.1.of 3

Hello guys, i got this code from an online tutorial i watched, it's about showing and hiding panel with a button clicked interchangeably, however, this doesn't seem to work rightly in unity 2017, Could anybody please suggest the right way to go about this, i'd be very grateful

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ShowHide : MonoBehaviour 
 {
     public GameObject panel;
     int counter;
 
     public void SwitchShowHide()
     {
         counter++;
 
         if (counter % 2 == 1)
         {
             panel.gameObject.SetActive (false);
         } 
         else
         {
             panel.gameObject.SetActive (true);
         }
     }
 
 }
 





Comment
Add comment · Show 2
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 Hellium · Sep 18, 2017 at 09:43 AM 0
Share

Are you sure you have specified the SwitchShowHide function in the OnClick event of your button (in the Inspector)?

avatar image zidav111 · Oct 28, 2020 at 01:49 PM 0
Share

I also have the same problem

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by UnityCoach · Sep 18, 2017 at 09:33 AM

This is a bit odd to use an integer to hold a true/false state.

I would rather to it this way :

 public class ShowHide : MonoBehaviour 
  {
      public GameObject panel;
      bool state;
  
      public void SwitchShowHide()
      {
          state = !state; 
          panel.gameObject.SetActive (state);
      }
  }
Comment
Add comment · Show 2 · 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 UnityCoach · Sep 18, 2017 at 09:35 AM 0
Share

Actually, panel is already the gameObject, so it would be :

 panel.SetActive (state);
avatar image Jeoyinc · Sep 18, 2017 at 10:04 AM 0
Share

I've tried this and many other but still doesn't work rightly, when i added a debug.log of the current (Boolean)state to know what's happening, it's as if both states of the (Boolean)state variable is called on every click i.e both true and false is called simultaneously onclick ins$$anonymous$$d of switching values for each click

avatar image
0

Answer by MrCringeGaming · Sep 18, 2019 at 09:41 PM

I'm using this code, but im running into the issue where they overlap when you click both buttons. I am trying to stop that from happening...

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class CloseOpenUI : MonoBehaviour
 {
     public GameObject upgPanel;
     public GameObject rbupgPanel;
 
 
 
     /* In order for this to work it must be attached to your 
      * gamemanager object & attached to the onclick function 
      * on the buttons
      */
 
     public void UpgOpenPanel()
     {
         if (upgPanel != null)
         {
             bool isActive = upgPanel.activeSelf;
             upgPanel.SetActive(!isActive);
         }
     }
 
     public void RbOpenPanel()
     {
         if (rbupgPanel != null)
         {
             bool isActive = rbupgPanel.activeSelf;
             rbupgPanel.SetActive(!isActive);
         }
     }
 
 }

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

175 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 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 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 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 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 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 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 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 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 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 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

Display loading indicator during onClick event? 0 Answers

Change trigger for OnClick to a joystick button 0 Answers

unity 2018.3.12f1 Button OnClick call function on another instance? of script 0 Answers

UI Buttons without blocking raycast 0 Answers

Button OnClick function? 1 Answer


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