Unity动画组件在嵌套的if语句中出现问题。

huangapple go评论77阅读模式
英文:

unity animator component breaks in nested if statements

问题

在第一个代码示例中,你的代码没有问题。但在第二个代码示例中,你遇到了一个问题,错误信息是“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?)”。这是因为正确的方法是使用 "GetBool" 和 "SetBool",而不是 "getBool" 和 "setBool"。

你可以将第二个代码示例中的这些行:

if(animator.getBool("hasWeapon")){
	animator.setBool("hasWeapon", false);
}else{
	animator.setBool("hasWeapon", true);
}

更改为:

if(animator.GetBool("hasWeapon")){
	animator.SetBool("hasWeapon", false);
}else{
	animator.SetBool("hasWeapon", true);
}

这应该解决你遇到的问题。

英文:

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

答案1

得分: -1

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

animator.SetBool("isWalk", true);

英文:

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

animator.SetBool("isWalk",true);

huangapple
  • 本文由 发表于 2023年5月7日 20:58:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76194080.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定