【问题标题】:Calling Objects from StateMachineBehaviour从 StateMachineBehaviour 调用对象
【发布时间】:2022-10-18 23:17:08
【问题描述】:

只是在此处创建此帖子,因为我在 Unity 论坛中没有得到任何回复。

我无法让这个脚本在状态机中工作。 我想让一层不活动,我想改变一个动画参数。 问题是 whis 脚本没有编译。 关于如何做到这一点的任何线索?

public class SnorHelmetRemove : StateMachineBehaviour
{
    protected GameObject headParent;
    protected Animator animator;
    protected bool checkHelmet;

    private void OnEnable() {
        animator = GameObject.GetComponent<Animator>();
        bool checkHelmet = animator.GetBool("IsWearingHelmet");
        headParent = GameObject.Find("HeadParent");
    }

    override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {

        Debug.Log("Helmet is " + checkHelmet);
        headParent.SetActive(false);
    }

    override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        if (checkHelmet) {
            Debug.Log("Helmet is " + checkHelmet);
            animator.SetBool("IsWearingHelmet",false);
        }   else {
            Debug.Log("Helmet is " + checkHelmet);
            animator.SetBool("IsWearingHelmet",true);
        }
    }
}

我在尝试编译时收到的错误在这一行 animator = GameObject.GetComponent&lt;Animator&gt;(); 错误消息是这样的:

非静态字段、方法或对象需要对象引用 属性 GameObject.GetComponent();

我遵循了 Unity 指南https://docs.unity3d.com/2022.2/Documentation/ScriptReference/Animator.GetBool.html 并且 getObject 类是小写的,但是如果我更改它仍然无法工作,并且我有这个错误:

当前上下文中不存在名称“gameObject”

有谁能够帮我?

【问题讨论】:

  • 脚本未编译。什么是编译错误以及在哪一行。请edit您的问题包括这些详细信息

标签: c# unity3d


【解决方案1】:

这是因为您正在使用 GameObject.getcomponent... 它不知道您要访问的游戏对象。如果动画师与此脚本位于同一对象上,则只需要 Get 组件...。

【讨论】:

    【解决方案2】:

    错误信息非常明显:GameObject.GetComponent不是static 方法。你宁愿需要一个实例GameObject 以获取附加到它的组件。

    但是,在您的情况下 - 您需要缓存的 animatorbool 字段做什么?

    在所有相关的状态消息中,无论如何您都会将动画师作为参数传入,并且可以在那一刻获取和设置布尔值。

    public class SnorHelmetRemove : StateMachineBehaviour
    {
        protected GameObject headParent;
    
        override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
        {
            // Cache this one and only fetch it again when needed
            if(!headParent) headParent = GameObject.Find("HeadParent");
    
            // Get CURRENT bool state
            var checkHelmet = animator.GetBool("IsWearingHelmet");
    
            Debug.Log("Helmet is " + checkHelmet);
    
            // I just assume this should use the flag accordingly, otherwise why does it exist at all?
            headParent.SetActive(checkHelmet);
        }
    
        override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
        {
            // Get CURRENT bool state 
            var checkHelmet = animator.GetBool("IsWearingHelmet");
    
            Debug.Log("Helmet is " + checkHelmet);
    
            // invert the bool
            checkHelmet = !checkHelmet;
    
            // Write back the new state
            animator.SetBool("IsWearingHelmet", checkHelmet);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-06
      • 2014-10-08
      • 2014-03-05
      • 2021-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多