Animator boolean problem
Hello everyone.
I've looked around Google and the Unity forums for quite a while without finding an answer to my problem.
I am using the latest version of Unity, in a 2D project.
I am currently trying to access an Animator boolean from a script. When I click on the bool from the Animator window, it works normally, but I can't toggle it with the script for some reason. I have another Animator bool system for walking animations, and it works.
The boolean's name is "TextBox Active", I am trying to toggle it on and off with script. Pressing on the Space key should open the text box, and close it if it's already open.
Here's my script :
using UnityEngine;
using System.Collections;
public class TextBoxAnimation : MonoBehaviour {
public bool textBoxActive;
void Start ()
{
textBoxActive = GameObject.Find("TextBox").GetComponent<Animator> ().GetBool ("TextBox Active");
}
void FixedUpdate ()
{
if (Input.GetKeyDown (KeyCode.Space))
{
textBoxActive = !textBoxActive;
}
}
}
This is the last thing I tried out before asking for help here. I have also tried to go with : "public Animator textBoxActive;" and using .SetBool("TextBox Active", true) and .SetBool("TextBox Active", false), but it didn't work either.
This problem occurs every time I try to create a toggle. Sometimes, one part of the code will work, but the bool won't come back from its "inverted" position. I always use "bool = !bool;" whenever I can, but it doesn't work.
Since I'm a complete newcomer to C#, I know that I am doing something wrong and not seeing it. My code doesn't give me error messages in the console.
Thanks in advance for your help.
Answer by PhantomSarcasm · Jul 15, 2016 at 02:21 PM
I began to test your code and does not effectively work to me, I saw the most prudent, I would use two variables or just one depending on how comfortable you feel.
two var
public Animator animacion;
public bool dos;
void Start () {
animacion = GetComponent<Animator> ();
}
void Update () {
if (Input.GetKeyDown (KeyCode.Space)) {
dos = !dos;
animacion.SetBool ("dos", dos);
}
}
with a var
public bool dos;
void Update () {
if (Input.GetKeyDown (KeyCode.Space)) {
dos = !dos;
this.gameObject.GetComponent<Animator>().SetBool ("dos", dos);
}
}
I hope it is to your liking, and forgive if you were looking for the solution to your ccodigo without having to use another. sorry
Answer by Jarod99 · Jul 15, 2016 at 03:11 PM
@PhantomSarcasm I used your "two var" method on another script that wasn't working for the same reasons as my Animator one, and it works perfectly now.
I'll test it on a new Animator script that I'll be creating from scratch and I'll edit this post when I'm done testing. I will also be trying the "one var" method and check it out. Thanks for the help!
Edit : The "two var" method also worked on the Animator script, and it is now flawless. I used it with "OnTriggerStay2D" and "Update", and there is no problem with it. Thanks again for the help.
you're welcome. I'm glad that it has been helpful. luck with your test.
Your answer
Follow this Question
Related Questions
PROBLEM WITH ANIM.SETBOOL NOT WORKING 0 Answers
Animation problem? 0 Answers
Can't play an animation from the Animator 1 Answer
stickman animation 1 Answer
Problems with using the same Animator Controller on multiple objects. 0 Answers