0
\$\begingroup\$

game engine:unity3d

scripting language: unityscript

code:

   var setmenu:GameObject; 

  var settings_menu:boolean;

   function Start()        {

   settings_menu=false; 

  }
   function settingButtonClick(){ 

  if(settings_menu==false)
  { 
  setmenu.SetActive(true);
    settings_menu=true; 

  }
         else
  { 
  setmenu.SetActive(false);      
   settings_menu=false; 
   }
 }

The problem is I have to click the UI button twice to SetActive to become false for the 1st time. But after that when I again activate and then press to deactivate it works fine with single click.

\$\endgroup\$
2
\$\begingroup\$

At the Start() you're setting the variable settings_menu to false, then you're checking whether that variable is false or not. Since it is false at first, it will set the menu active on your first press of the button.

The problem you have is not the function, but rather the fact that you're setting a preset value instead of checking whether the menu is active or not at start. What you should be doing is this:

Start()
{
 settings_menu = setmenu.activeSelf; //set the state of the menu to the variable instead.
}
\$\endgroup\$
1
\$\begingroup\$

Insted of trying in if statement you can try this

setmenu.SetActive(!settings_menu);
settings_menu=!settings_menu;

with out if statement and i think this should work fine

\$\endgroup\$
  • \$\begingroup\$ Still not working \$\endgroup\$ – DudeMan May 24 '17 at 5:16
  • \$\begingroup\$ But thanks this reduces the script length \$\endgroup\$ – DudeMan May 24 '17 at 13:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.