- Home /
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
so you're attaching this script to only 1 object right?
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);
}
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
Follow this Question
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