- Home /
UI Button ON/Off
I am using the UI canvas, How do I get the object(monster) to turn off using the same button.
public class monsterSwitch : MonoBehaviour {
public GameObject monster;
public void changeMonsterOn (string changeToOn) {
monster.SetActive(true);
}
public void changeMonsterOff (string changeToOff) {
monster.SetActive(false);
}
}
Comment
Best Answer
Answer by alok-kr-029 · Mar 09, 2015 at 06:52 AM
private bool toggle =false ;
public void changeMonsterOn ()
{
if(!toggle)
{
monster.SetActive(true);
toggle= true;
}
else
{
monster.SetActive(false);
toggle= false;
}
}
Hope this will work if you just want to toggle with the UI button
Answer by novcraft · Aug 31, 2018 at 04:53 AM
Easiest way is this
private bool toggle = false; // global variable default is off
public void changeMonsterOn ()
{
toggle = !toggle; // if toggle is false then it would be true, if toggle is true then it would be false like on and off
monster.SetActive(toggle); // pass the toggle value
}
Answer by Edisyo · Aug 27, 2019 at 07:27 AM
Also this works, but if needed, remember to put start value
if(value)
{
value = false;
return;
}
if(!value)
{
value = true;
return;
}