Animator.getbool problems

why does the following not work?

void Update () {
		if (added && !anim.GetBool("attacking")) {
						weapondamage -= 300;
				}
		if (anim.GetBool("attacking")) {
						weapondamage += 300;
						added = true;

				} else {
			added = false;		
		}


	}

In another script the bool is set:

if (Input.GetKeyDown (KeyCode.J)) {
						
			anim.SetBool("attacking", true);



				}

when the animation ends, this function is triggered:

public void AnimationEnded(){

		anim.SetBool ("attacking", false);
	

	}

While the animation plays, I do see the checkmark, so the bool is true. But when anim.GetBool(“attacking”) is used it doesn’t get the true value, it says even though the checkmark is there it still is false, but the animation is playing so it must be true

From the code, everything seems fine. You set the bool, you access the bool. No obvious error…

So… I cannot really give you the correct answer to your problem, but here is how I would proceed to find the error:

  • Get a nice and easy way to reproduce the problem. You said, you see the bool gets checked in the “Animator Window” when you press the button. For example, add a log next to the GetBool to immediately print out its value for easiest recognition. This way you can check in a couple of seconds whether the GetBool works now (press play, press J, compare the checkbox with the log)
  • Make a backup of your project now. (If you use Source Control system, do a checkin on another branch)
  • Now start stripping down your project. Delete gameObject after gameObject, remove Scripts and components and assets. Simplify assets too (like removing states from the Animator). Just keep removing complexity where you think it should not affect the problem.
  • After every couple of deletions, make another backup (but keep the previous one)!
  • Then test whether the problem still occurs. If it doesn’t, think about what you deleted last. If you still don’t have a clue what could cause it, restore from your previous backup and delete a bit less things or remove other things.
  • Finally at some time you will arrive at a so few complexity that the problem will spring into your face. Some misspelled text, some forgotten link… You will see!

This is the general way to approach problems that appear so simple and should definetely work, but just don’t.

By the way, you could also go the opposite (sometimes faster):

  • Start with an empty project
  • Keep adding stuff (scripts, assets, gameObjects) from your problem-project to the new project until you experience the problem

Hope it helps…

I had a similar problem and here was how I solved it.

In my case the problem lied in the “anim” object. The anim object did not belong to the active Player Game Object in the game window but to a prefab of that player somewhere in the assets folder.

Before running your game, make sure that the anim object in your script is an instance of the Animator Controller that is currently active in the game.

(I know it’s two late wink wink , but this answer may help someone else) …

had same problem came to workaround with syncronizing condition to set rather then reading result out bool