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>()

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值