Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Animator "Hash" breaking code. fix?

Discussion in 'Animation' started by AncientNomad, Mar 31, 2022.

  1. AncientNomad


    Mar 29, 2022
    public class AnimationStateController : MonoBehaviour
    Animator animator;
    int isWalkingHash;
    int isRunningHash;

    // Start is called before the first frame update
    void Start()
    animator = GetComponent<Animator>();

    isWalkingHash = Animator.StringToHash("isWalking");
    isRunningHash = Animator.StringToHash("isRunning");


    // Update is called once per frame
    void Update()
    bool isrunning = animator.GetBool(isRunningHash);
    bool isWalking = animator.GetBool(isWalkingHash);
    bool forwardPressed = Input.GetKey("w");
    bool runPressed = Input.GetKey("left shift");

    if (!isWalking && forwardPressed)
    animator.SetBool(isWalkingHash, true);

    if (isWalking && !forwardPressed)
    animator.SetBool(isWalkingHash, false);

    if (!isrunning && (forwardPressed && runPressed))
    animator.SetBool(isRunningHash, true);

    if (isrunning && (!forwardPressed || !runPressed))
    animator.SetBool(isRunningHash, false);

    so everything seems to work perfectly before i added the "isRunningHash" i was able to walk and start the animation but now that ive added the "isRunningHash' i get an error that lets me play scene but not activate my "run" animation. the error is

    "Parameter 'Hash 1239866031' does not exist. UnityEngine.Animator:GetBool (int)