unity-game-engine - GetComponent 要求请求的组件 'AudioSource[]' 派生自 MonoBehaviour 或 Component 或者是一个接口(interface)

标签 unity-game-engine speech-to-text

我实现了 IBM Watson 的语音转文本,因此当我说“跳跃”/“愤怒”时,我的角色将播放音频剪辑。但是我收到此错误,这导致角色无法对我的语音触发器使用react。

错误消息:

Unity Exception ArgumentException: GetComponent requires that the requested component 'AudioSource[]' derives from MonoBehaviour or Component or is an interface.

我的CharacterController.cs:

   using UnityEngine;

    public class CharacterController : MonoBehaviour
    {

        // Use this for initialization

        public Animator anim;

        public AudioSource[] _audio;

        void Start()
        {

        }

        // Update is called once per frame
        void Update()
        {

            anim = GetComponent<Animator>();
            _audio = GetComponent<AudioSource[]>();
        }

        public void CharacterActions(string ActionCommands)
        {
            ActionCommands = ActionCommands.Trim();
            switch (ActionCommands)
            {
                case "jump":
                    anim.Play("jump", -1, 0f);
                    _audio[0].Play();
                    break;
                case "anger":
                    anim.Play("rage", -1, 0f);
                    _audio[1].Play();
                    break;

                default:
                    anim.Play("idle", -1, 0f);
                    break;

            }


        }
    }

最佳答案

您不能使用 GetComponent 获取所有 AudioSource 对象的数组,因为 Unity 将搜索类型为 AudioSource[] 的组件,而不是AudioSource ,它不存在。要获取您必须执行的所有 AudioSource 对象的数组

_audio = GetComponents<AudioSource>();

相反。

关于unity-game-engine - GetComponent 要求请求的组件 'AudioSource[]' 派生自 MonoBehaviour 或 Component 或者是一个接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51663812/

相关文章:

unity-game-engine - Unity 中可以存在这个着色器吗?

c# - 如何从 C++ DLL 导出全局变量?

android - 有没有办法在 Windows 程序中模拟 Android 应用程序?

c# - 如何修改轨道脚本以在 Unity3d 中触摸的按钮上启动

go - 突然,用于语音到文本的 Go 语言 Google Cloud API 无法正常工作!/lib64/libc.so.6 : version `GLIBC_2.32' not found

android - 如何使用 Androids 的语音与音频示例文件一起发短信

适用于盲人的 JavaScript 语音转文本

firebase - Unity Firebase 自动创建匿名帐户

audio - Google Cloud Speech:单词开始时间

javascript - 如何停止Microsoft认知服务-语音-sdk-js以停止手动监听?