Unity3D 关于声音MissingComponentException报错

如果你遇到报错如下:

MissingComponentException: There is no 'AudioSource' attached to the "Level-2-bg" game object, but a script is trying to access it.
You probably need to add a AudioSource to the game object "Level-2-bg". Or your script needs to check if the component is attached before using it.

原因是因为你在给对象添加音频属性的时候,在脚本里面使用了音频源

  gameObject.GetComponent<AudioSource>().PlayOneShot(errorClip);

可是改游戏对象没有 AudioSource 这个组件,所以就用不了,就如同一个对象没有脚本你却要活的这个脚本一样,肯定会报错

gameObject.GetComponent<Test>()

所以这里为了用这个组件里面的方法,就需要添加一个这样的组件。

这样就能够解决这个问题了。

posted on 2016-01-20 14:32  手撕高达的村长  阅读(2840)  评论(0编辑  收藏  举报

导航