【问题标题】:how to properly use getComponants to get a audio array?如何正确使用 getComponants 获取音频数组?
【发布时间】:2013-03-17 03:42:45
【问题描述】:

好的,所以我把这段代码压缩到了一行:

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 数组?

【问题讨论】:

    标签: audio unity3d unityscript


    【解决方案1】:

    显然,您必须使用此代码使 GetComponents() 返回类型(在本例中为 AudioSource):

    gameObject.GetComponents<AudioSource>();
    

    【讨论】:

      【解决方案2】:

      试试这个:

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

      【讨论】:

      • 那个给了我这个:Assets/scriptexplosion.js(7,34): BCE0048: Type 'System.Type' 不支持切片。不过这是个好主意。
      猜你喜欢
      • 1970-01-01
      • 2018-09-25
      • 1970-01-01
      • 2022-07-26
      • 1970-01-01
      • 2016-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多