我实现了IBM Watson的“语音转文本”,因此当我说“跳” /“怒”时,我的角色将播放音频剪辑。但是我遇到了这个错误,这阻止了角色对我的语音触发器做出反应。
错误消息:
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;
}
}
}
答案 0 :(得分:2)
您不能使用GetComponent
来获取所有AudioSource
对象的数组,因为Unity将搜索类型为AudioSource[]
而不是AudioSource
的类型的组件,该组件不存在。要获取所有AudioSource
对象的数组,
_audio = GetComponents<AudioSource>();
相反。