Unity Animator 组件在嵌套 if 语句中中断

unity animator component breaks in nested if statements

提问人:Sefa Aydemir 提问时间:5/7/2023 更新时间:5/7/2023 访问量:34

问:

尝试在嵌套的 if else 语句中使用组件方法,但是在 2nd depth 编译器无法识别该组件 我假设嵌套语句是问题所在;

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);
    }
}

此代码工作正常,没有问题

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);
    }
}

但是,此代码会抛出“Animator 不包含'getBool/setBool'的定义,并且找不到接受'Animator'类型的第一个参数的可访问扩展方法'getBool/setBool'(您是否缺少 using 指令或程序集引用? 仅在第 18、19、21、26 行、第 25 行和第 34 行没有引发错误 我意识到我可以简单地使用多个if语句,如true&&true,false&&true,false&&false等。 但是我想了解这个问题

的 C# Unity-Game-Engine

评论

1赞 BugFinder 5/7/2023
大小写算朋友。getBool 不是 GetBool。setBool 不是 SetBool

答:

-1赞 Fatih Parlağı 5/7/2023 #1

您应该使用大写字母“S”的 SetBool,使用大写字母“G”的 GetBool,就像您在行中使用它一样

animator.SetBool("isWalk",true);

评论

0赞 Sefa Aydemir 5/7/2023
这很尴尬,但谢谢