Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug PlayOneShot() plays the wrong clip, but only the first time

Discussion in 'Audio & Video' started by RainerEngblom, Oct 4, 2023.

  1. RainerEngblom

    RainerEngblom

    Joined:
    Sep 10, 2020
    Posts:
    9
    Ok, this is really bugging me out. I have a function in my HUDManager to play sounds. It works fine, except the first time I send in a throwSound. The console says the name of the clip is the correct one, but the sound I hear is a complitely different one the first time I throw. After that everything is fine. The code is super simple:

    Code (CSharp):
    1. public void PlaySound(AudioClip newSound)
    2.     {
    3.         print(newSound.name);
    4.         myAudio.volume = 1f;
    5.         myAudio.pitch = 1f;
    6.         myAudio.PlayOneShot(newSound);
    7.     }
     
  2. RainerEngblom

    RainerEngblom

    Joined:
    Sep 10, 2020
    Posts:
    9
    Ok, I found the reason for this. I have another function in HUDManager:
    Code (CSharp):
    1. public void PlaySound(AudioClip newSound, float volume, float pitch)
    2.     {
    3.         myAudio.volume = volume;
    4.         myAudio.pitch = pitch;
    5.         myAudio.PlayOneShot(newSound);
    6.     }
    And something is calling it at Start with another sound and pitch=0, which seems to leave the clip dormant in my AudioSource. Then I set pitch=1 in my PlaySound() and the first sound starts playing.
     
    KalUnity3D likes this.
  3. KalUnity3D

    KalUnity3D

    Unity Technologies

    Joined:
    Jul 17, 2020
    Posts:
    15
    @RainerEngblom glad to hear you fixed it. It's always worth whacking in a few Debug.Log messages, even against objects/clips, to check for unintended logic.