Search code examples
c#unity-game-enginenested

unity animator component breaks in nested if statements


Trying use component methods in nested if else statements however at 2nd depth compiler doesn't recognize the component I'm assuming nested statements are the issue since;

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class animation : MonoBehaviour
{
    Animator animator;
    // Start is called before the first frame update
    void Start()
    {
        animator=GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKey("w")){
            animator.SetBool("isWalk",true);
        }
        else
            animator.SetBool("isWalk",true);
    }
}

This code works fine with no problems

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class animation : MonoBehaviour
{
    Animator animator;
    // Start is called before the first frame update
    void Start()
    {
        animator=GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKey("1")){
            if(animator.getBool("hasWeapon")){
                animator.setBool("hasWeapon",false);
            }else{
                animator.setBool("hasWeapon",true);
            }
        }

        else if(Input.GetKey("w")){
            animator.SetBool("isWalk",true);
            if(Input.GetKey("shift")){
                animator.setBool("isRun",true);
                animator.setBool("isWalk",false);
            }else{
                animator.setBool("isRun",false);
            }
        }
        else
            animator.SetBool("isWalk",false);
    }
}

This code however throws "Animator does not contain a definition for 'getBool/setBool' and no accessible extension method 'getBool/setBool' accepting a first argument of type 'Animator' could be found (are you missing a using directive or an assembly reference?" only on lines 18,19,21,26, lines 25 and 34 throw no error I realize i can simply use multiple if statements like true&&true, false&&true, false&&false etc. however i want to understand this problem


Solution

  • You should use SetBool with capital 'S' and GetBool with capital 'G' like you used it in line

    animator.SetBool("isWalk",true);