- Home /
Simple if condition in function not turning off
Hi everyone,
I'm sitting with what must be a pretty simple problem that I just cant seem to figure out.
My code:
function OnGUI () {
SwitchOnObject(characterHead);
}
function SwitchOnObject ( object : GameObject) {
var objectSwitchedOn : boolean = false;
if (!objectSwitchedOn) {
object.SetActiveRecursively(true);
objectSwitchedOn = true;
Debug.Log(object.active);
}
The if condition wont turn off even though I set the boolean to true, any ideas?
Answer by Cripple · Sep 05, 2012 at 09:00 PM
Your variable is declared inside the function. So its value is reseted each time the function is called.
You have to declare it outside the function as a private member of your class.
You should learn the base of programming
Good luck ;)
I dont know how I overlooked that, I think I was trying to create some kind of local variable inside the scope of the function, stupid mistake on my part.
I always create my variables on the base class, just thought there was a way to create a flag inside a function that doesnt reset.
Your answer
![](https://koobas.hobune.stream/wayback/20220613080656im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
boolean flicker 3 Answers
Hiding OnGUI? 1 Answer
OnGUI Script Not Working With Boolean... 1 Answer
GUI Box appear when you are close to something? 1 Answer
If statement with OnGUI 2 Answers