- Home /
Question by
jennes · Sep 18, 2012 at 06:24 PM ·
toggle button
Toggle button don't dissapear..
hello, this is my code
private var toggleState1 : boolean = false;
function OnGUI() { toggleState1 = GUI.Toggle(Rect(430, 210, 100, 30), toggleState1, "Sound on/off"); AudioListener.pause = toggleState1; }
function Awake() { DontDestroyOnLoad(this); }
But if I load a new scene my button will be in all the scenes.. how do I destroy the button but dont destroy if it is true?
Comment
Best Answer
Answer by Code_Of_Honour · Sep 19, 2012 at 02:46 AM
Your problem is that you're always doing the "DontDestroyOnLoad", no matter what toggleState1's value is. Notice how it's now in an "if" statement in the code below ;)
Hope this helps!
private var toggleState1 : boolean = false;
function OnGUI()
{
GUI.Rect(430, 210, 100, 30); //place actual GUI rectangle code here.
}
function Awake()
{
if(toggleState1 == true)
{
DontDestroyOnLoad(this);
}
}
If you don't $$anonymous$$d, please vote up my answer so people who see this will know it worked for you. ;)
Your answer
![](https://koobas.hobune.stream/wayback/20220613081212im_/https://answers.unity.com/themes/thub/images/avi.jpg)