- Home /
PlayOneShot keeps playing for ever (loops)
for easily(er) playing some soundeffects, I made a small script:
SoundController:
private static var Singleton : GameObject;
static function GetController() {
if (!Singleton) Create(); return Singleton; }
private static function Create() { Singleton = new GameObject("SoundObject"); Singleton.AddComponent(SoundScript); Singleton.AddComponent(AudioSource); }
static function Play(sound:AudioClip,volume: float)// = 1.0F) : void {
if (!Singleton) Create(); Singleton.audio.PlayOneShot(sound,volume);; }
So I can just do
SoundController.Play(sound,1);
Only problem is it keeps playing the sound over and over as soon as it first starts.
Why did I do wrong ?
EDIT:
answer: Nothing. I don't think I actually did anything wrong, cause it works now. Could it be a caching problem of some sort in Unity? (no idea how that would have this effect but I just don't know what else it could be)
Answer by Mike 3 · Jan 10, 2011 at 09:29 PM
Your whole script could be replaced with the much easier:
static function Play(sound:AudioClip, volume:float)
{
AudioSource.PlayClipAtPoint(sound, Vector3.zero, volume); //could replace the position with Camera.main.transform.position or somesuch if you want it to play at a constant volume
}
You don't need a gameobject nor audiosource to use PlayClipAtPoint, since it's a static function, whch makes it easier for doing what you're trying to achieve
Either way, if that doesn't fix it, then the bug is likely in the class calling it
Incorrect. You need an instance to use PlayOneShot. However you probably refer to AudioSource.PlayClipAtPoint.
But I agree that the script probably can be replaced all together.
Oh true, this is why you shouldn't answer on Answers after 40 hours working without sleep! Fixed it :D
yup, that works. I didn't know PlayClipAtPoint existed, that makes my entire script useless. thanks.
I don't really get the position parameter though, it doesn't seem to have any effect.
Your answer
Follow this Question
Related Questions
Audio after Audio Loop 1 Answer
my audio is not looping even when it is set to loop 5 Answers
Can I play multiple AudioSources from one gameobject? 8 Answers
My AudioClip wont loop even though loop is true 1 Answer
My soundclip is noisy? 2 Answers