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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Bug Sound plays more than once.

Discussion in 'Scripting' started by Kokomeant, Aug 30, 2023.

  1. Kokomeant

    Kokomeant

    Joined:
    Dec 23, 2022
    Posts:
    13
    Hello, First of all I want to apolgize for my English :).
    I'm trying to add sounds to my game but I discoverd a bug that when my player perform "JumpState == 2" then for some reason the sound plays more than once:

    SoundManager:
    Code (CSharp):
    1. public class SoundManager : MonoBehaviour {
    2.     public static SoundManager Instance { get; private set; }
    3.     public enum Sound {
    4.         Alarm,
    5.         Jump,
    6.         SuperJump,
    7.         SuperJump2,
    8.     }
    9.     private AudioSource audioSource;
    10.     private Dictionary<Sound, AudioClip> soundAudioClipDictionary;
    11.     private void Awake() {
    12.         Instance = this;
    13.  
    14.         audioSource = GetComponent<AudioSource>();
    15.  
    16.         soundAudioClipDictionary = new Dictionary<Sound, AudioClip>();
    17.  
    18.         foreach (Sound sound in System.Enum.GetValues(typeof(Sound))) {
    19.             soundAudioClipDictionary[sound] = Resources.Load<AudioClip>(sound.ToString());
    20.         }
    21.     }
    22.     public void PlaySound(Sound sound) {
    23.         Debug.Log(sound.ToString());
    24.         audioSource.PlayOneShot(soundAudioClipDictionary[sound]);
    25.     }
    26. }
    PlayerController:
    Apply input in Update():

    Code (CSharp):
    1.     private void ApplyInput() {
    2.  
    3.         h = Input.GetAxisRaw("Horizontal");
    4.  
    5.         desiredQuantity = h;
    6.  
    7.         HandleMovementPerSecond();
    8.  
    9.         currentQuantity = Mathf.MoveTowards(currentQuantity, desiredQuantity, MovementPerSecond * Time.deltaTime);
    10.  
    11.         if (onGrounded()) {
    12.             if (Input.GetKey(KeyCode.Space)) {
    13.                 if (onJumpState == 0 && Rb2D.velocity.y == 0) {
    14.                     if (Mathf.Abs(Rb2D.velocity.x) < speedToMultiply - 2) {
    15.                         Debug.Log("Velocity = 0");
    16.                         onJumpState = 1;
    17.                         SoundManager.Instance.PlaySound(SoundManager.Sound.Jump);
    18.                     } else {
    19.                         Debug.Log("Velocity > 0");
    20.                         onJumpState = 2;
    21.                     }
    22.                 }
    23.             }
    24.         } else {
    25.             if (onJumpState != 0) onJumpState = 0;
    26.             Debug.Log("AirState");
    27.         }
    28.     }
    I'm trying to figure out what's the problem here and it seems that everything really executes one times as supposed to be but only the sound executes more than once, for an example if my player perform JumpState == 2 that supposed to be the SuperJump the JumpState == 1 that supposed to be normal jump starts execute more than once when the entrie block only once.

    Has anyone encountered such a problem?
    And how I can fix it?
    Thanks if advance! :)
     
  2. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    first make sure you're only loading 1 sound manager:
    Code (CSharp):
    1. public class SoundManager : MonoBehaviour
    2. {
    3.     private void Awake()
    4.     {
    5.         Instance = this;
    6.         print("SoundManager made");
    7.         audioSource = GetComponent<AudioSource>();
    8.     }
    9. }
    Just to double check, as you never assume, always make sure.

    But pretty sure your error is:
    if (Input.GetKey(KeyCode.Space))

    As you would be better off using GetKeyDown() so that logic only reads once. Just GetKey() runs multiple times for how ever many frames the button is pressed(usually used for movement).
     
    Kokomeant likes this.
  3. Kokomeant

    Kokomeant

    Joined:
    Dec 23, 2022
    Posts:
    13
    Thank you very much for quick replay, first of all I made my sound manager make sure that only one copy exists, and also I found what caused the issue, the character movement itself was not the issue it was my sound itself somehow I missed it up..

    Thank you very much for highlighting that my sound manager didn't make sure that there is only one copy, and thank you very much for your help and very quick reponse I really appriciate it :)