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?