英文:
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论