- Home /
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
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.
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
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.
Ok thanks, just wanted to make sure I wasn't missing something obvious
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
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;
}
Why would you declare a new variable when the game object has a perfectly good one (activeInHierarchy) for you already?
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.
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?
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
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.
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;
}
}
Your answer
Follow this Question
Related Questions
GUI Button to toggle true false when pressed 1 Answer
button pressed twice / Conversation text skip 5 Answers
GUI problem 1 Answer