我实现了 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/