Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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
1
Question by ptblk · Jul 31, 2015 at 05:49 PM · gui-button

Click button to open pop up, click SAME button to close

Hi simple function I am looking for, yet unity just doesn’t seem to support it at all via the on click gui

Have a button, when I click it, a popup shows. Simple enough used onclick with setactive bool. Now, where I really don’t understand why this doesn’t exist is to be able to set it that when I click it again, the popup will close. I have tried putting another set active below that and set that to false, but that doesn’t work.

If I add an event trigger, it has the option for on pointerenter and pointerexit as well as mouseup and mousedown. Now why is there no onclick and on click "again" functionality? I want this for mobile so I can’t make use of pointers or mouse clicks as those don’t exist in mobile

Thanks

Comment
Add comment
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

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Dave-Carlile · Jul 31, 2015 at 05:53 PM

You want to toggle the gameobject between active and inactive right? You can toggle a boolean value between true and false by using value = !value. So to toggle whether or not something is active...

 gameObject.SetActive(!gameObject.activeInHierarchy);


If activeInHierarchy is true then !activeInHierarchy return false. If it's false then !activeInHierarchy returns true. This has the effect of toggling the active value between true and false, based on its current value. Or you might want to use activeSelf instead of activeInHierarchy.

So you can just call that in the button's OnClick event to toggle the state.

Now, if the toggling script is a component on the object you're toggling, once you set it to inactive the script will no longer run since scripts don't run on inactive objects. The script needs to live on an object that isn't inactivated, such as the button itself.

Comment
Add comment · Show 6 · 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 Dave-Carlile · Jul 31, 2015 at 07:21 PM 0
Share

Curious as to why the down vote?

avatar image ptblk · Aug 01, 2015 at 03:15 PM 0
Share

So there is no way to do this without scripting?

$$anonymous$$y understanding was that the UI Onclick trigger on the button would start off false (no tick) and I would set the popup to not be inactive. Then when i click the button the bool changes to true and popup appears, which works fine. But then I would think that clicking it again would set it back to inactive which is the issue, as it doesnt

avatar image Hexer · Aug 01, 2015 at 03:43 PM 0
Share

you do have to add a little bit of code, this is because of the fact the UI system doesn't work as you discribe or wished it should work.

Also I think you have set a statement for what the button must do if the bool is true. But you haven't wrote any line of code for when the bool is false.

avatar image ptblk · Aug 02, 2015 at 10:49 AM 0
Share

Ok thanks, just wanted to make sure I wasn't missing something obvious

avatar image ptblk · Aug 03, 2015 at 08:47 AM 0
Share

can you please give the complete code, one line as a designer makes no real difference as I still still with a piece of the puzzle and no clue where to put this. might be common knowledge to you but not for everyone

Show more comments
avatar image
1

Answer by Hexer · Jul 31, 2015 at 05:59 PM

I don't see the problem. You just use a bool to get what you want. If the bool is true get the popup. When it is clicked again, set the bool to false. I guess you are also using the unityEngine.UI system

//b ool start stage, if the popup is closed set this to true else false.

 bool X = false; 
 
 public void click(){
 if(X == true){
 //popup
 X = false;
 }
 if(X ==false){
 //close it
 X = true;
 }
Comment
Add comment · Show 4 · 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 Dave-Carlile · Jul 31, 2015 at 07:22 PM 0
Share

Why would you declare a new variable when the game object has a perfectly good one (activeInHierarchy) for you already?

avatar image Hexer · Jul 31, 2015 at 07:28 PM 0
Share

I don't know why but I find this easier to understand, it is just that I have always done it this way. But your method is also perfectly fine.

avatar image ptblk · Aug 03, 2015 at 08:44 AM 0
Share

Is this code incomplete? It doesnt make any difference to what the ui already isnt doing for me which is to set bool active and inactive when i click the same button. Been sitting 2 days on this now with zero results, for something that should be able to be done in 2 $$anonymous$$s. Please this is really holding my progress back.

Also doesnt allow me to tell the button which popup i want to open and close?

avatar image ptblk · Aug 04, 2015 at 01:48 PM 0
Share

NO result in any working answer, because Dave feels that people that come here to ask questions are as well versed in c# as he is. WHY WOULD I CO$$anonymous$$E HERE IF I $$anonymous$$NEW THE ANSWER! common sense says people come here because they don’t know the answers and are beginners. THUS giving me some half ass answer means nothing and is just a lazy attempt at providing any for of help

avatar image
0

Answer by Mani_2020 · Apr 08, 2020 at 03:02 PM

public GameObject MultiViewerMenu;

public void OpenMultiViewerMenu() { if(MultiViewerMenu!= null) {

         bool isActive = MultiViewerMenu.activeSelf;
     
     
        
        MultiViewerMenu.SetActive(!isActive);
     }
 }

I am using this code ,its clearly popping the panel but if i click it again its flickering and not responding.

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 unity_JhBPq7w2igO2Dw · Mar 20 at 04:15 PM

Here you go! When you press a button a popup appears, then when you press the same button the same popup disappear

 private bool clickedOnce = false;
 
 public void PopUpPanel()
 {
         if (!clickedOnce ) 
         {
             Panel_InfoHelp.SetActive(true);
             clickedOnce = true;
         }
         else
         {
             Panel_InfoHelp.SetActive(false);
             clickedOnce = false;
         }
 }




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

7 People are following this question.

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

Related Questions

GUI Button to toggle true false when pressed 1 Answer

button pressed twice / Conversation text skip 5 Answers

GUI problem 1 Answer

how can i change the gui button on runtime?? 1 Answer

How can you do a custom GUIStyle image position? 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