如何正确使用getComponants获取音频数组?

时间:2013-03-27 05:23:31

标签: audio unity3d unityscript

好吧,所以我得到了这个代码,我缩小到一行:

gameObject.GetComponents(AudioSource)[0].audio.Play();

这一行给了我这个错误:

InvalidCastException: Cannot cast from source type to destination type.
scriptexplosion+$Start$4+$.MoveNext () (at Assets/scriptexplosion.js:7)

我的问题是如何将组件数组转换为AudioSource数组?

2 个答案:

答案 0 :(得分:1)

显然你必须使用这段代码让GetComponents()返回类型(在本例中为AudioSource):

gameObject.GetComponents<AudioSource>();

答案 1 :(得分:0)

试试这个:

gameObject.GetComponents(AudioSource[0]).audio.Play();