Hi everyone,
I am not able to link the sound effects to the game objects or scripts I need them to be.
What I’ve done so far is:
setting up a slider for sfx (working properly)
setting up a player for sfx (working as well) - I probably could have used only one player for music and sfx, but wasn’t too sure
SFX Slider and player are linked to the playerprefscontroller and optioncontroller.
Now, I should link the sfx in the scripts where I need them to be. I’ve tried every way I could think of and rewatched all the lessons regarding sound/music.
In the health script below, the lines commented out were the last way I’ve tried and how I could test that my slider works, but I get the (red) message that my deathSound is not assigned.
Health Script:
public class Health : MonoBehaviour
{
[SerializeField] float health = 100f;
[SerializeField] GameObject deathVFX = default;
//[SerializeField] AudioSource deathSound; - this should be an array which didn't work either
public void DealDamage(float damage)
{
health -= damage;
if (health <= 0)
{
TriggerDeathVFX();
//PlayDeathSound();
Destroy(gameObject);
}
}
private void TriggerDeathVFX()
{
if(!deathVFX)
{
return;
}
GameObject deathVFXObject = Instantiate(deathVFX, transform.position, transform.rotation);
Destroy(deathVFXObject, 1f);
}
/* private void PlayDeathSound()
{
deathSound.volume = PlayerPrefsController.GetSFXVolume();
}*/
}
PlayerPrefs:
public class PlayerPrefsController : MonoBehaviour
{
const string MUSIC_VOLUME_KEY = "music volume";//from master to music volume
const string SFX_VOLUME_KEY = "sfx volume";// from master sfx to sfx volume
const string DIFFICULTY_KEY = "difficulty";
const float MIN_VOLUME = 0f;
const float MAX_VOLUME = 1f;
const float MIN_DIFFICULTY = 0f;
const float MAX_DIFFICULTY = 2f;
public static void SetMusicVolume(float volume)
{
if (volume >= MIN_VOLUME && volume <= MAX_VOLUME)
{
Debug.Log("music volume");
PlayerPrefs.SetFloat(MUSIC_VOLUME_KEY, volume);
}
else
{
Debug.LogError("Music volume is out of range");
}
}
public static float GetMusicVolume()
{
return PlayerPrefs.GetFloat(MUSIC_VOLUME_KEY);
}
public static void SetSFXVolume(float volume)
{
if (volume >= MIN_VOLUME && volume <= MAX_VOLUME)
{
Debug.Log("sfx volume");
PlayerPrefs.SetFloat(SFX_VOLUME_KEY, volume);
}
else
{
Debug.LogError("SFX volume is out of range");
}
}
public static float GetSFXVolume()
{
return PlayerPrefs.GetFloat(SFX_VOLUME_KEY);
}
public static void SetDifficulty(float difficulty)
{
if (difficulty >= MIN_DIFFICULTY && difficulty <= MAX_DIFFICULTY)
{
PlayerPrefs.SetFloat (DIFFICULTY_KEY, difficulty);
}
else
{
Debug.LogError("Difficulty setting is not in range");
}
}
public static float GetDifficulty()
{
return PlayerPrefs.GetFloat(DIFFICULTY_KEY);
}
}
OptionController:
public class OptionController : MonoBehaviour
{
[SerializeField] Slider musicSlider = default;
[SerializeField] float defaultVolume = 0.8f;
[SerializeField] Slider sFXSlider = default;
[SerializeField] float defaultSFXVolume = 0.5f;
[SerializeField] Slider difficultySlider = default;
[SerializeField] float defaultDifficulty = 0f;
// Start is called before the first frame update
void Start()
{
musicSlider.value = PlayerPrefsController.GetMusicVolume();
sFXSlider.value = PlayerPrefsController.GetSFXVolume();
difficultySlider.value = PlayerPrefsController.GetDifficulty();
}
// Update is called once per frame
void Update()
{
var musicPlayer = FindObjectOfType<MusicPlayer>();
if (musicPlayer)
{
musicPlayer.SetVolume(musicSlider.value);
}
else
{
Debug.LogWarning("No music player found....did you start from splash screen?");
}
var loadSound = FindObjectOfType<SFXPlayer>();
if (loadSound)
{
loadSound.SetVolume(sFXSlider.value);
}
else
{
Debug.LogWarning("No sfx found....did you start from splash screen?");
}
}
public void SaveAndExit()
{
PlayerPrefsController.SetMusicVolume(musicSlider.value);
PlayerPrefsController.SetSFXVolume(sFXSlider.value);
PlayerPrefsController.SetDifficulty(difficultySlider.value);
FindObjectOfType<LevelLoader>().LoadMainMenu();
}
public void SaveandReturn()// for implementing maybe a jump back to the exact same spot during the game and save the playerprefs
{
PlayerPrefsController.SetMusicVolume(musicSlider.value);
PlayerPrefsController.SetSFXVolume(sFXSlider.value);
PlayerPrefsController.SetDifficulty(difficultySlider.value);
//FindObjectOfType<LevelLoader>().LoadPreviousScene();// this needs to be fixed, problem being too many jumps...maybe
}
public void SetDefaults()
{
musicSlider.value = defaultVolume;
sFXSlider.value = defaultSFXVolume;
difficultySlider.value = defaultDifficulty;
}
}
SFXPlayer:
public class SFXPlayer : MonoBehaviour
{
AudioSource audioSource;
// Start is called before the first frame update
void Start()
{
DontDestroyOnLoad(this);
audioSource = GetComponent<AudioSource>();
audioSource.volume = PlayerPrefsController.GetSFXVolume();
}
public void SetVolume(float volume)
{
audioSource.volume = volume;
}
}
Sorry for the huge amount of code:(
Any help is appreciated
cheers