Search Unity

Question Sound not playing

Discussion in 'Scripting' started by aiyan21, Mar 14, 2023.

  1. aiyan21

    aiyan21

    Joined:
    Mar 1, 2023
    Posts:
    21
    Hello, I've been trying to look for errors but I really can't find it. the sound was working fine but when I started doing the volume slider player prefs thing, the music stopped playing. I hope you guys can help me cause I'm near our school deadline TT

    Sound Manager:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Audio;
    5. using UnityEngine.UI;
    6. using UnityEngine.SceneManagement;
    7. using System;
    8.  
    9. public class Soundmanager : MonoBehaviour
    10. {
    11.     public static Soundmanager Instance;
    12.     public Sound[] musicSounds, sfxSounds;
    13.     public AudioSource musicSource, sfxSource;
    14.  
    15.     [SerializeField]
    16.     public Slider musicSlider = null;
    17.     [SerializeField]
    18.     public Slider sfxSlider = null;
    19.  
    20.     public void Awake()
    21.     {
    22.  
    23.         if (Instance == null)
    24.         {
    25.             Instance = this;
    26.             DontDestroyOnLoad(gameObject);
    27.         }
    28.         else
    29.         {
    30.             Destroy(gameObject);
    31.             return;
    32.         }
    33.  
    34.         if (SceneManager.GetActiveScene().buildIndex == 0)
    35.         {
    36.             PlayMusic("Main");
    37.         }
    38.         else if (SceneManager.GetActiveScene().buildIndex == 1)
    39.         {
    40.             PlayMusic("Intro");
    41.         }
    42.         else if (SceneManager.GetActiveScene().buildIndex == 2)
    43.         {
    44.             PlayMusic("Quiz");
    45.         }
    46.         else if (SceneManager.GetActiveScene().buildIndex == 6)
    47.         {
    48.             PlayMusic("Results");
    49.         }
    50.     }
    51.     public void Start()
    52.     {
    53.         LoadValues();
    54.     }
    55.  
    56.     public void musicVolume(float volume)
    57.     {
    58.         musicSource.volume = musicSlider.value;
    59.         PlayerPrefs.SetFloat("MUSICVOLUME", musicSource.volume);
    60.  
    61.         sfxSource.volume = sfxSlider.value;
    62.         PlayerPrefs.SetFloat("SFXVOLUME", sfxSource.volume);
    63.     }
    64.  
    65.     void LoadValues()
    66.     {
    67.         musicSource.volume = PlayerPrefs.GetFloat("MUSICVOLUME");
    68.         musicSlider.value = musicSource.volume;
    69.         AudioListener.volume = musicSource.volume;
    70.  
    71.         sfxSource.volume = PlayerPrefs.GetFloat("SFXVOLUME");
    72.         sfxSlider.value = sfxSource.volume;
    73.         AudioListener.volume = sfxSource.volume;
    74.     }
    75.  
    76.     public void PlayMusic(string name)
    77.     {
    78.         Sound s = Array.Find(musicSounds, x => x.name == name);
    79.  
    80.         if (s == null)
    81.         {
    82.             Debug.Log("Sound not found :(");
    83.             return;
    84.         }
    85.         else
    86.         {
    87.             musicSource.clip = s.clip;
    88.             musicSource.Play();
    89.         }
    90.     }
    91.  
    92.     public void StopMusic(string name)
    93.     {
    94.         Sound s = Array.Find(musicSounds, x => x.name == name);
    95.  
    96.         if (s == null)
    97.         {
    98.             Debug.Log("Sound not found :(");
    99.         }
    100.         else
    101.         {
    102.             musicSource.clip = s.clip;
    103.             musicSource.Stop();
    104.         }
    105.     }
    106.  
    107.     public void PlaySFX(string name)
    108.     {
    109.         Sound s = Array.Find(sfxSounds, x => x.name == name);
    110.  
    111.         if (s == null)
    112.         {
    113.             Debug.Log("Sound not found :(");
    114.         }
    115.         else
    116.         {
    117.             sfxSource.PlayOneShot(s.clip);
    118.         }
    119.     }
    120. }
    I also tried putting the PlayMusic() on my scene switcher because it worked before I did the volume slider thing. but it stopped working and I moved it to my sound manager, however, it still didn't play the music.

    Scene Switcher:

    Code (CSharp):
    1.  
    2.     public void Awake()
    3.     {
    4.         /*
    5.         if (SceneManager.GetActiveScene().buildIndex == 0)
    6.         {
    7.             Soundmanager.Instance.PlayMusic("Main");
    8.         }
    9.         else if (SceneManager.GetActiveScene().buildIndex == 1)
    10.         {
    11.             Soundmanager.Instance.PlayMusic("Intro");
    12.         }
    13.         else if (SceneManager.GetActiveScene().buildIndex == 2)
    14.         {
    15.             Soundmanager.Instance.PlayMusic("Quiz");
    16.         }
    17.         else if (SceneManager.GetActiveScene().buildIndex == 6)
    18.         {
    19.             Soundmanager.Instance.PlayMusic("Results");
    20.         }
    21.         */
    22.  
    23.     }
    edit: i found the culprit. the audio source had its volume at 0. but i dont know why is it at 0, how can i define the volume at start?
     
    Last edited: Mar 14, 2023
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    First make sure all your code is running like expecting. Are you adding more Debug calls? Are those calls being hit? Do you see the audioclip being assigned to your audio source? etc. If all that checks out, make sure you didn't mute your game window inside Unity. Along the menu bar of the Game tab there is an option to mute all audio. Make sure you haven't accidentally hit that.
     
  3. aiyan21

    aiyan21

    Joined:
    Mar 1, 2023
    Posts:
    21
    debug calls are working fine. i made sure audio sources were not disabled, the game wasn't muted and also outside unity. sounds in audio source were also showing.

    edit: i found the culprit. the audio source had its volume at 0. but i dont know why is it at 0, how can i define the volume at start?
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    aiyan21 likes this.
  5. aiyan21

    aiyan21

    Joined:
    Mar 1, 2023
    Posts:
    21
    It is on max, however when i play the game it goes back to 0 : (

    edit : oh nvm its good now, thank you!!
    but I'm encountering a new problem where when i go back to my options menu (after proceeding to another scene), the volume slider is not working anymore. the music is still playing tho
     
    Last edited: Mar 14, 2023
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Start looking for errors, stuff blocking the slider, make sure it's set to interactable, the script isn't disabled, etc. There could be a ton of reasons.