Hey,
I am implementing an on/off switch using the code below.
The problem is that the switch gets off but it doesnt get on i.e. it goes inside the inner ‘if’ but it doesnt go inside the ‘else’. Any ideas?
if (GUI.Button(new Rect(145, 50, 36, 30), "")) //Music
{
if(isOn_M)
{
music = music_off;
isOn_M = false;
}
else
{
music = music_on;
isOn_M = true;
}
}
if (GUI.Button(new Rect(145, 50, 36, 30), "")) //Music
{
isOn_M = !isOn_M;
if(isOn_M)
music = music_off;
else
music = music_on;
}
?
This might sounds obvious, but did you instantiate the boolean beforehand ?
Or even, are you changing it in between the clicks ? is it accessed anywhere else ?
EDIT
Ok, You need to initialise the boolean outside the function.
put this line :
bool isOn_M = false;
inside the class, but outside of functions
(btw, i’m assuming this is c#, i might be wrong)
The reason being that a variable declared inside a function only exists within that function. once the function is done executing, the variable is “disposed of”, to free memory. You need the variable to be part of your class. so that it “lives” as long as your class exists.