- Home /
Play sound from an array, not random, and only once
Hi, I'm having trouble to make something "simple" work properly. I've created a small script to create an array of sound (only 2 sounds for now but may grow further) and play one of the sound accordingly to the value of a variable named "combo". For exemple, if combo is equal to 1.4f, it plays the sound n°1 once; if combo => 1.7f it plays sound n°2 only once. But it's actually not working and plays both sounds over and over when combo is => 1.7f. I could'n find a proper solution for now.
here's my script:
using System.Collections;
using UnityEngine;
public class ComboSounds : MonoBehaviour {
public AudioClip[] audioClipArray;
private AudioSource source;
void Start () {
source = gameObject.GetComponent<AudioSource> ();
}
void Update () {
if (ComboController.combo == 1.4f)
{
source.clip = audioClipArray[0];
source.PlayOneShot (source.clip);
source.Play();
}
if (ComboController.combo >= 1.7f)
{
source.clip = audioClipArray[1];
source.PlayOneShot (source.clip);
source.Play();
}
}
}
Floating point equality comparison isn't guaranteed. I think you mean to do something like this:
float combo = ComboController.combo;
if (combo >= 1.7f)
{
source.clip = audioClipArray[1];
}
else if (combo >= 1.4f )
{
source.clip = audioClipArray[0];
}
else
{
return;
}
source.PlayOneShot (source.clip);
Although, I think you'd also need something there to keep track of which "tier" combo you're working on next, to keep the sounds unique to those new player situations and not constantly playing throughout their experience.
Answer by iHaveReturnd · Sep 13, 2018 at 07:43 PM
You're running this in the update, so it makes sense that you're getting the looping audio when the value is 1.7f. Every frame that it's 1.7f its going to call your source.Play() and start the audio again You don't want both of those there either, you likely only one source.PlayOneShot with how you've described it, so remove source.Play() as dan_wipf suggested. dan_wipf's comments are still important changes to make, even if its still not working.
If you only want them to play once ever, you could use a flag type system. public class ComboSounds : MonoBehaviour {
public AudioClip[] audioClipArray;
private AudioSource source;
private bool played14 = false;
private bool played17 = false;
void Start () {
source = gameObject.GetComponent<AudioSource> ();
}
void Update () {
if (ComboController.combo == 1.4f && played14 == false)
{
played14 = true;
source.clip = audioClipArray[0];
source.PlayOneShot (source.clip);
source.Play();
}
if (ComboController.combo >= 1.7f && played17 == false)
{
played17 = true;
source.clip = audioClipArray[1];
source.PlayOneShot (source.clip);
source.Play();
}
}
}
You could also reset those variables to allow them to play again later if you wanted. Realistically if you're only wanting this event to happen once, you shouldn't check for it in the update. Some other event should be calling an audio play function when that specific thing happens.
There's a few other ways you could go about it depending on what your end goals are but hopefully that helps some.
If you're still having it play when it shouldn't it'd be helpful to see your inspector view of it, and the settings of the audio file in the inspector too.
Answer by dan_wipf · Sep 13, 2018 at 04:44 PM
delete the phrase source.Play(); and in inspector check that playonawake and loop in your audiosource is false(unchecked)..
I tried but I have still the same problem unfortunately, the first sound doesn't even play when "combo" == 1.4
my bad, i think you’re calling the function playoneshot multiplebtimes. because the bool stays true until the value changed.. you have to make a workaround, so it’s going to be a one time event!.. hope you get an idea
Answer by Corentin_Fousset-Martial · Sep 13, 2018 at 09:31 PM
Thanks dan_wipf and thanks iHaveReturnd ! Both advises helped me to get rid of the problem ! I've learned another thing today !
Your answer
Follow this Question
Related Questions
How to get decibel's value of game's sound? 0 Answers
AudioSource won't play 2 Answers
Audio: -3db automatic attenuation on any audio playing? 0 Answers
Question about audio (AudioSource). My ingame sound doesn't sound like the original audio file? 3 Answers
Make the audio manager select from an array of sounds 1 Answer