- Home /
Bool Parameter does not persist through code deactivation/reactivation -- except if set in Editor!
For some reason, when I set a bool mecanim parameter as "true" through code, that setting disappears (becomes false) upon deactivating/reactivating the object through code. HOWEVER, if that parameter was set to "true" through the state machine editor (and then deactivated/reactivated through code), its setting persists through both deactivation AND reactivation (in my case, this is the desired behavior -- except I want it to do this through code too!)
Any idea how to get the setting through code to persist through deactivation/reactivation too?
Or is this a bug?
If so, is there any workaround? (I'm using 4.6.1 if that helps!) I need it to be able to remember the bool setting from code in order to fork my state transition at the start of the animation the next time it plays (i.e. after being reactivated). I'm playing an intro animation only once, then it should transition directly to the other animation state every time after that (upon being reactivated), making this decision initially upon gameobject reactivation!
Answer by awesomedata · Jan 01, 2015 at 01:14 AM
Finally found a solution to my own problem to work around this bug, in case someone else is having this same issue:
Use OnEnable() and OnDisable() events to store the parameter temporarily and set it again upon reactivation (i.e. use OnEnable() to set it back as soon as the object is reactivated).
Answer by shriya · Dec 29, 2014 at 06:16 AM
HI, I don't find any need to keep your boolean public . Try with this:
private static yourbool;
and maintain everything through code. I hope it helps.
Sorry, but my boolean is a parameter inside the mecanim state machine that I set/get through the animator component. I need the mecanim state machine to be able to access it internally so it can deter$$anonymous$$e (via transition conditions) what animation to play.
That said, I'd hate to rewrite mecanim via code just to swap animations one time. And, even if I just used the mecanim system entirely through code, there's still no guarantee the value will persist when the object is deactivated since I'd still be using the animator component and mecanim's internal systems! D:
Perhaps the problem lies in the SetActive() function itself??
Is there any way to set the bool in a roundabout way (i.e. by modifying how the internal editor code sets the bools somehow maybe?)
Answer by Wandersoul · Dec 29, 2014 at 05:57 PM
Bools are false by default. Assuming you are using C# you can define the initialized value by changing your declaration from :
public bool flag;
to:
public bool flag = true;
This will cause the bool to use true by default instead of false;
I'm using mecanim state machine parameters to set a Bool (and Javascript) using "animator.SetBool("mybool");" through code.
That said, I'm not finding a way around the issue with your code that allows the parameter to be remembered through deactivation/reactivation with your answer.
Any ideas on how to go about this with your solution (or at least an alternative solution)? What would the code need to look like to do this for Javascript?
Your answer

Follow this Question
Related Questions
Animator Bool: How do you use it? 1 Answer
Error : Animator has not been initialized. UnityEngine.Animator:SetBool(String, Boolean) 1 Answer
Animator parameter stuck as True 4 Answers
How to get Animator.GetBool to work as a parameter in OnCollisionEnter in Unity (c#)? 0 Answers
Can animation parameters only be changed in scripts? 1 Answer