When I compile the game in the editor, I can hear the music all the time (which is good too.)
But why can’t I change the volume of the BGMusic?
Info: “snd_BGMusic” is a AudioSource where got the both scripts: “DontDestroyAudio” and “SettingsMenu” Inside of the components.
Script1 “DontDestroyAudio”:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DontDestroyAudio : MonoBehaviour
{
private void Awake()
{
GameObject[] MusicObj = GameObject.FindGameObjectsWithTag("tag_BGMusic");
if(MusicObj.Length > 1)
{
Destroy(this.gameObject);
}
DontDestroyOnLoad(this.gameObject);
}
}
Script2 “SettingsMenu”:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SettingsMenu : MonoBehaviour
{
private AudioSource audio_Sound, audio_Voice, audio_BGMusic;
public GameObject ObjectMusic;
// "0.5f" ist die startposition von den Audioslindern:
private float BGMusicValue = 0.3f, VoiceValue = 0.3f, SoundValue = 0.3f;
private void Start()
{
audio_Sound = GetComponent<AudioSource>();
audio_Voice = GetComponent<AudioSource>();
ObjectMusic = GameObject.FindWithTag("tag_BGMusic");
audio_BGMusic = ObjectMusic.GetComponent<AudioSource>();
}
// ==================== Volume Sliders ====================
public void SetSoundVolume(float onclick_Soundvol)
{
}
public void SetVoiceVolume(float onclick_Voicevol)
{
}
public void SetBGMVolume(float onclick_BGMusicvol)
{
Debug.Log(BGMusicValue);
//SlinderFloat in den zwischenFloatWert: "BGMusicValue"
BGMusicValue = onclick_BGMusicvol;
//Zwischenwert: "BGMusicValue" in die audio Source updaten:
audio_BGMusic.volume = BGMusicValue;
}
}