- Home /
Stop audioclip from another script
Hi guys, I need to stop an audioclip that will be played from an empty gameobject that has a script attached to it.
The attached script will play the music and another script attached to another gameobject will be stop it.
How can I accomplish this?
I could use an audiosource instead, but if I did, how could I invoke audio.stop() from the second script attached to the second gameobject?
Thanks.
Answer by getyour411 · Aug 11, 2015 at 12:56 AM
public void StopThisClip(AudioSource aSource, Clip clip) {
if(aSource.clip.isPlaying())
aSource.Stop();
}
pseudocode
@getyour411 Thank you for answering. Your answer point me into the right direction. Ins$$anonymous$$d of an AudioClip
I used an AudioSource
with a sound file attached to the object. Now, from the second script I can external$$anonymous$$usic.GameObject<$$anonymous$$iniGame1$$anonymous$$usic>().StopThisClip;
and external$$anonymous$$usic.GameObject<$$anonymous$$iniGame1$$anonymous$$usic>().audio.clip = GameOver;
and external$$anonymous$$usic.GameObject<$$anonymous$$iniGame1$$anonymous$$usic>().audio.clip.play();
. But, the game over music is cut. The reason is explained here, but I can't find a way to fix that and allow the music file to fully play...
On GameOver, are you doing a scene transition? If so, you probably want DontDestroyOnLoad on the object controlling music/ambient/SFX