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

delete the phrase source.Play(); and in inspector check that playonawake and loop in your audiosource is false(unchecked)…

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.

Thanks dan_wipf and thanks iHaveReturnd ! Both advises helped me to get rid of the problem ! I’ve learned another thing today !