AudioSource Play() and PlayOneShot() problems. I need help

I don’t understand why running a sound effect has to be so frustrating. Anyway…

I have a simple sound process. And this sound process is set to play only once if the player crosses the finish line. But it’s not working properly.

On the surface you’d think that the AudioSource component would be very simple to use, but it’s really frustrating that even the most basic functions of the component are malfunctioning.

The problem is that I have different problems with the Play() and PlayOneShot() functions.

1- Play()

  • The Play() function plays the audio in a looped manner even though the loop property is turned off in the AudioSource component. As soon as the sound ends, it runs again and again. Thinking that cause this is according to the structure of the function. Then I use the PlayOneShot() function.

2- PlayOneShot()

  • The PlayOneShot() function, as the name suggests, is supposed to play the sound only once (well, I guess. Naturally. Naturally. I guess.) In my code, if I use the PlayOneShot function, the sound plays when the condition occurs, but it overlaps an unbelievable number of times and becomes very annoying.

In this case, you could say that the AudioSource is running too fast in my condition (or the condition is running slow / or fast), which causes PlayOneShot() to play the sound more than once. (The bool variable _finished was used as a flag, but the sound still works as i said).

So what should I do to make only 1 sound effect work? Do I really have to write dozens of lines of code to make only 1 sound effect work?

public class AudioManager : MonoBehaviour
{

// ------------------------------------------- REFFERENCES ------------------------------------------

[Header("Audio Source")]
[SerializeField] public AudioSource _audioSource;

[Header("Sounds")]
[SerializeField] AudioClip _finishConfettiSound;
[SerializeField] AudioClip _jumpSound;
[SerializeField] AudioClip _levelRepeatSound;

// ------------------------------------------- INSTANCE ------------------------------------------

public static AudioManager _instance;

private void Awake()
{
    if(_instance == null)
    {
        _instance = this;
    }
    else
    {
        Destroy(_instance);
    }
    _audioSource.clip = _finishConfettiSound;    
}
// ------------------------------------------- METHODS ------------------------------------------

    public void PlayFinishConfettiSound() // Calling in PlayerStartFinishHandler.cs
    {
        if(!_audioSource.isPlaying) _audioSource.PlayOneShot(_audioSource.clip);
    }
}
public class PlayerStartFinishHandler : MonoBehaviour
{

    public static PlayerStartFinishHandler _instance;

    [Header("Components")]
    [SerializeField] CinemachineFreeLook _freeLook;
    [SerializeField] Transform _finishLine;
    [Space]

    [Header("UI Refferences")]
    [SerializeField] private Image _fillerImage;
    [Space]

    [Header("Finish Operations")]
    [SerializeField] ParticleSystem[] _confettis;
    [SerializeField] UnityEvent _openNextPanel;

    [Header("Calculations")]
    [SerializeField] private Vector3 _startLinePosition;
    [SerializeField] private Vector3 _finishLinePosition;
    [SerializeField] private float _distance;
    [SerializeField] private float _fullDistance;

    [SerializeField] public bool _finished;

    private void Awake()
    {
        if(_instance == null)
        {
            _instance = this;
        }
        else
        {
            Destroy(_instance);
        }
    }

    private void Start()
    {
        _finishLinePosition = _finishLine.position;
        _fullDistance = GetDistance();
    }
    private float GetDistance() 
    {
      return (_finishLinePosition - PlayerController._instance.transform.position).sqrMagnitude ;
    }
    private void UpdateProgressFill(float value)
    {
       _fillerImage.fillAmount = value ;
    }
    private void Update () {

      if (PlayerController._instance.transform.position.z <= _finishLinePosition.z) {
         float newDistance = GetDistance () ;
         float progressValue = Mathf.InverseLerp (_fullDistance, 0f, newDistance) ;

         UpdateProgressFill (progressValue);
         _finished = false;
      }
      else
      {
        _fillerImage.fillAmount = 1;
        _finished = true;
        if(_finished == true)
        {
            FinishOperations();
        }
      }
   }

   private void FinishOperations()
   {
        for(int i =0; i < _confettis.Length; i++)
        {
            if(!_confettis[i].isPlaying)
            _confettis[i].Play(true);
        }
        AudioManager._instance.PlayFinishConfettiSound();
        InputManager._Instance._canGetInput = false;
        InputManager._Instance._canSpace = false;
        _freeLook.Follow = null;
        _openNextPanel.Invoke();
        _finished = false;
   }
}

Replace lines 64 to 68 with:

	if(_finished == false)
	{
		FinishOperations();
	}

and replace line 84 with:

	_finished=true;

Did this fix your issue @kaanguliletisim?

Hello, yes, my problem is solved. It’s seems its my fault. Peace.

1 Like