Sound not playing

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:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System;

public class Soundmanager : MonoBehaviour
{
    public static Soundmanager Instance;
    public Sound[] musicSounds, sfxSounds;
    public AudioSource musicSource, sfxSource;

    [SerializeField]
    public Slider musicSlider = null;
    [SerializeField]
    public Slider sfxSlider = null;

    public void Awake()
    {

        if (Instance == null)
        {
            Instance = this;
            DontDestroyOnLoad(gameObject);
        }
        else
        {
            Destroy(gameObject);
            return;
        }

        if (SceneManager.GetActiveScene().buildIndex == 0)
        {
            PlayMusic("Main");
        }
        else if (SceneManager.GetActiveScene().buildIndex == 1)
        {
            PlayMusic("Intro");
        }
        else if (SceneManager.GetActiveScene().buildIndex == 2)
        {
            PlayMusic("Quiz");
        }
        else if (SceneManager.GetActiveScene().buildIndex == 6)
        {
            PlayMusic("Results");
        }
    }
    public void Start()
    {
        LoadValues();
    }

    public void musicVolume(float volume)
    {
        musicSource.volume = musicSlider.value;
        PlayerPrefs.SetFloat("MUSICVOLUME", musicSource.volume);

        sfxSource.volume = sfxSlider.value;
        PlayerPrefs.SetFloat("SFXVOLUME", sfxSource.volume);
    }

    void LoadValues()
    {
        musicSource.volume = PlayerPrefs.GetFloat("MUSICVOLUME");
        musicSlider.value = musicSource.volume;
        AudioListener.volume = musicSource.volume;

        sfxSource.volume = PlayerPrefs.GetFloat("SFXVOLUME");
        sfxSlider.value = sfxSource.volume;
        AudioListener.volume = sfxSource.volume;
    }

    public void PlayMusic(string name)
    {
        Sound s = Array.Find(musicSounds, x => x.name == name);

        if (s == null)
        {
            Debug.Log("Sound not found :(");
            return;
        }
        else
        {
            musicSource.clip = s.clip;
            musicSource.Play();
        }
    }

    public void StopMusic(string name)
    {
        Sound s = Array.Find(musicSounds, x => x.name == name);

        if (s == null)
        {
            Debug.Log("Sound not found :(");
        }
        else
        {
            musicSource.clip = s.clip;
            musicSource.Stop();
        }
    }

    public void PlaySFX(string name)
    {
        Sound s = Array.Find(sfxSounds, x => x.name == name);

        if (s == null)
        {
            Debug.Log("Sound not found :(");
        }
        else
        {
            sfxSource.PlayOneShot(s.clip);
        }
    }
}

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:

    public void Awake()
    {
        /*
        if (SceneManager.GetActiveScene().buildIndex == 0)
        {
            Soundmanager.Instance.PlayMusic("Main");
        }
        else if (SceneManager.GetActiveScene().buildIndex == 1)
        {
            Soundmanager.Instance.PlayMusic("Intro");
        }
        else if (SceneManager.GetActiveScene().buildIndex == 2)
        {
            Soundmanager.Instance.PlayMusic("Quiz");
        }
        else if (SceneManager.GetActiveScene().buildIndex == 6)
        {
            Soundmanager.Instance.PlayMusic("Results");
        }
        */

    }

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?

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.

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?

Since you have the audiosource object, just set it’s volume. Normally before even going into play mode, you should just have it at max.

1 Like

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

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.