0
\$\begingroup\$

I noticed on a tutorial for unity someone used the code:

private Animator animator;
private bool isRunning = false;

While further on, did:

animator.SetBool("isRunning", true)

Why could you not just do

isRunning = true

if it is in the same class?

\$\endgroup\$
1
\$\begingroup\$

These two lines do different things.

animator.SetBool("isRunning", true)

This sets The Animator's isRunning parameter to true - which is likely to affect which animation state it selects / blends to within its animator control graph.

isRunning = true;

Sets this script's isRunning member variable to true. We can't see much of how this script functions or even what it is called in your example, but it sounds like it's used for local book-keeping of gameplay state, so that your control script knows it's in the "running" state and can make control decisions differently informed by that fact.

isRunning = true will not automatically change your animator's parameter states or affect how it selects animations to play. animator.SetBool("isRunning", true) will not set your control script's member variable to affect its control logic.

Depending on the design of your control script, you might need to set both the animator parameter and the control script's member variable in the same places, to keep the internal control model consistent with the animated "view" of that model state.

\$\endgroup\$
1
  • \$\begingroup\$ I see, it was the boolean within the anmator rather than the script \$\endgroup\$ – Ronnie Lightweightbaby Coleman Dec 9 '19 at 10:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.