Hey.
In the Main Menu of my game, when the player clicks “Quit” and then “Yes” on the dialog window I want my music to fade out and when that’s done = Application.Quit();
The music fades out just fine, but my MainMenuClickEvents.cs never detects when musisFaded is true in FadeMusic.cs…
Here’s my code:
FadeMusic.cs
using UnityEngine;
using System.Collections;
public class FadeMusic : MonoBehaviour {
[HideInInspector] public bool musicFaded = false;
[Range(0f, 1f)] public float audioFadedVolume = 0f;
public float fadeSpeed = 6f;
private float originalVolume;
void Start()
{
musicFaded = false;
originalVolume = this.GetComponent<MainMenuClickEvents> ()._audioSource.volume;
Debug.Log ("Original volume: " + originalVolume);
}
public void FadeOutMusic(GameObject _objectSource)
{
StartCoroutine (Fade_Music (_objectSource.GetComponent<AudioSource>()));
}
IEnumerator Fade_Music(AudioSource _aSource)
{
while (_aSource.volume > audioFadedVolume) {
_aSource.volume -= .1f * (Time.deltaTime * fadeSpeed);
if (_aSource.volume <= audioFadedVolume) {
_aSource.volume = audioFadedVolume;
musicFaded = true;
Debug.Log("Music has been faded...");
} else {
musicFaded = false;
}
yield return 0;
}
}
}
MainMenuClickEvents.cs:
The “Yes” button in my dialog windows calls this function:
public void Confirm_Quit()
{
m_state = menuState.None;
quitDialog.Hide ();
this.GetComponent<FadeMusic> ().FadeOutMusic (this.musicObject);
StartCoroutine (HideAndQuit ());
}
Here’s the IEnumerator HideAndQuit():
IEnumerator HideAndQuit()
{
while (!this.GetComponent<FadeMusic>().musicFaded) {
if(this.GetComponent<FadeMusic>().musicFaded) {
Debug.Log ("MainMenu: Music is faded...");
break;
}
yield return 0;
}
}
Should I put this musicFaded = True in the Update instead?