set animator bool c#

i got this script, i made it, but i have no idea what im doing, so i need some help :slight_smile:

it isnt working sadly

void Awake()
    {
		open = 0;
		anim = GetComponent<Animator>();
    }
	
	void Update () 
	{
		if(Input.GetButtonDown("Use") && open == 1)
         {
          open = 0;
          anim.SetBool("open", false);
         }
         else if(Input.GetButtonDown("Use") && open == 0)
         {
          open = 1;
          anim.SetBool("open", true);
         }
	}

What is it even supposed to do? This looks like gibberish.

–

i have an open/ close animation in my animator, this script is attached to the gate i want opend and closed, for now, i want my script, when i press use, to set the bool in my animator to true or false if it already is true

–

without your mecanim structure it will be hard to help you. Cause maybe your problem comes from your Mecanim model ?

–

i have an animator on my gate, same gate where my script is. in the animator i got a controller called OpenDoor. in there i made a bool called open. if i turn the bool true while playing the game, the animation plays in the game, but i dont know how to get to it through the script

–

it's strange cause I do the same thing in C# and it works for me. I mean the animator.SetBool( "MyBool", boolValue )

–

1 Answer

1

The problem that I can see is that the variables you are setting are not being declared:

//Declared the variables for use by Awake AND update
var open;
var anim;

void Awake()
 {
 open = false;
 anim = GetComponent<Animator>();
 }

void Update()
 {
 //Improved this part by the use of inverted booleans :D
 if(Input.GetButtonDown("Use"))
  {
  open = !open;
  anim.SetBool("open", open);
  }
 }

wont i have to use private bool open; private Animator anim; instead of var open and var anim?

–

Well, I wanted to give you some basic code. I would have added private and given them types had I knew if you wanted other scripts to alter them.

–

is that the types i would use? :) cus they arent working :/

–