unity3d - GetComponent 要求请求的组件 'AudioSource[]' 派生自 MonoBehaviour 或 Component 或者是一个接口
问题描述
我实现了 IBM Watson 的 Speech-to-Text,所以当我说“jump”/“anger”时,我的角色会播放一个音频剪辑。但是我收到了这个错误,这会阻止角色对我的语音触发器做出反应。
错误信息:
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>();
反而。
推荐阅读
- python - 如何使用 python 脚本和 bigsuds 过滤 F5 监视器
- php - Laravel:如何创建 hasManyThrough 多态多对多关系?
- r - 可以从exp(coef(glm(...)))制作优势比图的R函数?
- android - 在 NDK19 上链接 ACE 静态库时出现不支持的动态 reloc r_arm_rel32 错误
- apache-spark - 如何在 spark-submit 中排除包?
- c# - 如何修复“找不到类型或命名空间名称“PlayerManager”(您是否缺少 using 指令或程序集引用?
- generics - Kotlin 中有没有办法将内联函数和 when 表达式与扩展函数结合起来?
- keycloak - keycloak 网守不会阻止任何请求
- asp.net-core - 在 Azure DevOps Pipeline 中使用阶段:意外值“阶段”
- ruby-on-rails - 我怎么知道它正在调用哪个类和函数?