- Home /
Script looses attached AudioSource when level is restarted
I attach two AudioSources in the inspector onto my script. The method PauseGame() is called when player click the pause button, which pauses the attached audio. When the player dies you restart the level by clicking the restart button which calls another script that loads the level using Application.LoadLevel("Level1");
My problem is, every time the level is reloaded both the AudioSources that i attached in the inspector are now missing and when i hit the pause button it obviously does not work because of the missing attached AudioSources. Why are the AudioSources dissapearing and how may i fix this?
public AudioSource[] music;
public bool paused;
void Start () {
paused = false;
}
public void PauseGame()
{
paused = !paused;
if(paused)
{
Time.timeScale = 0;
music[0].Pause();
music[1].Pause();
}
else if(!paused)
{
Time.timeScale = 1;
music[0].UnPause ();
music[1].UnPause();
}
}
Answer by mauri11 · Oct 04, 2017 at 06:05 AM
To answer my own question. One can not find an AudioSource if the GameObject is disabled which was causing the issue for me.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Sound on Airtap 0 Answers
Cannot play a disabled audio source? 1 Answer
Calling an Audio Source on one game object from a script on another game object..? 1 Answer