unity3d GetComponent要求请求的组件“AudioSource[]”派生自MonoBehaviour或Component或者是接口

xe55xuns  于 2023-01-09  发布在  iOS
关注(0)|答案(2)|浏览(183)

我实现了IBM Watson的语音到文本功能,所以当我说"jump "/" anger"时,我的角色会播放一段音频剪辑。但是我遇到了这个错误,它阻止了角色对我的语音触发做出React。
错误信息:

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

我的角色控制器. 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;

            }

        }
    }
wlzqhblo

wlzqhblo1#

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

_audio = GetComponents<AudioSource>();

而不是。

d7v8vwbk

d7v8vwbk2#

What worked for me: TMP_Text[] texts = newGo.GetComponentsInChildren<TMP_Text>(); Look that it is Component(s) not only Component.

相关问题