I have already checked other questions here on Stack Overflow and the Internet but couldn't find something similar.
I have a "Settings Scene" where I want to toggle a specific audio clips in other scenes (for example I want to mute countdown timer sound only from settings scene (scene 2) in the game scene (scene 7)).
In the Game Scene with the countdown timer, I have an AudioClip attached and I want to access it from the Settings Scene and send timerHandler.ToggleMuteTimer(false)
Here is some code:
public class TimerSoundHandler : MonoBehaviour {
public GameObject audioOnIcon;
public GameObject audioOffIcon;
private TimerHandler timerHandler;
void Start()
{
//timerHandler = GameObject.FindWithTag("TimerTAG").GetComponent<TimerHandler>();
//timerHandler = GameObject.FindGameObjectWithTag("TimerTAG");
timerHandler = FindObjectOfType<TimerHandler>();
SetSoundState();
}
public void ToggleSound()
{
if (PlayerPrefs.GetInt("TimerMuted", 0) == 0)
{
PlayerPrefs.SetInt("TimerMuted", 1);
}
else
{
PlayerPrefs.SetInt("TimerMuted", 0);
}
SetSoundState();
}
public void SetSoundState()
{
if (PlayerPrefs.GetInt("TimerMuted", 0) == 0)
{
UnmuteSound();
}
else
{
MuteSound();
}
}
private void UnmuteSound()
{
//timerHandler.ToggleMuteTimer(true);
audioOnIcon.SetActive(true);
audioOffIcon.SetActive(false);
}
private void MuteSound()
{
//timerHandler.ToggleMuteTimer(false);
audioOnIcon.SetActive(false);
audioOffIcon.SetActive(true);
}
}
And this is the TimerHandler script:
public class TimerHandler : MonoBehaviour {
public Text timerText;
public AudioClip timerClipSound;
[Range(0,3600)]
[SerializeField]
private float totalTime = 300f;
private bool timeUp = false;
private AudioSource audioSource;
private void Awake()
{
audioSource = GetComponent<AudioSource>();
}
private void Update()
{
if (timeUp)
return;
totalTime -= Time.deltaTime;
string minutes = ((int)totalTime / 60).ToString("00");
string seconds = (totalTime % 60).ToString("00");
timerText.text = minutes + ":" + seconds;
if (totalTime <= 0)
{
timeUp = true;
audioSource.mute = true;
}
}
public void ToggleMuteTimer(bool value)
{
audioSource.mute = value;
}
}
Any ideas how to access and edit a gameobject that is not even instantiated yet?