- Home /
Issue went away.
Oneshot only plays once?
Very simple code
public AudioClip soundToPlay;
public string tags;
void OnCollisionEnter (Collision collision) {
string[] splitTags = tags.Split(',');
for(int i = 0; i <= splitTags.Length-1; i++)
{
audio.PlayOneShot(soundToPlay);
}
The first collision, it plays, the second and thereafter, it does not. If I change it to audio.play() and put the audio clip in the audio source, it works fine. But I want to apply multiple audio clips to a game object this way so why is this not working? Is there a better way?
Is this supposed to play different sounds?
The way it is now, if the sound is $$anonymous$$OO and tags is "arf,bong,score" then your code will play $$anonymous$$OO $$anonymous$$OO $$anonymous$$OO (three times at once, so one very loud moo.) Because the loop just says to say $$anonymous$$OO for each word in tags.
It plays one sound per tag. So for example, if i have a ball if it hits a wall, or floor it plays the bounce sound, I have another that plays a different sound if the ball hits another ball, etc.
So technically the engine only grabs the one tag of the object it impacts, so it will only play the sound the one time for the impact. But I need it to play it again the next time the ball bounces. Right now it is playing the first time the ball bounces, and nothing after that.
I see in the code I pasted I missed this line. if(collision.gameObject.tag == splitTags[i])
I assure you it is there in my normal code.
If a line can "fall out" of the middle of a paste, maybe some other lines have also fallen out and broken your game logic.
$$anonymous$$aybe add `Debug.Log("play sound");` next to each `audio.Play` to check if the sound or the code is the problem (w/Collapse unchecked in the error window.)
How odd. I think it may have been my sound system. Last night it was not working when I went to bed, today it works and I changed nothing.
Answer by dotJack · Sep 23, 2012 at 10:57 AM
Are you sure the OnCollisionEnter calls it out properly each time?
If yes, you could try to instantiate a GameObject every time it is called, set it up with an AudioSource and a AudioClip, call it's play and then destroy the entire gameObject after length of AudioClip.
Building your own PlayOneShot that way.
Follow this Question
Related Questions
How to fix my reload sound? 0 Answers
Need help with AudioSources 1 Answer
audioclip is not working 0 Answers