Problems with playing a AudioSource

Hey guys,

So I am trying to play a AudioSource only if a variable from another script is true/false. I tried to search the internet, but sadly nothing did help.

EDIT: DID solve the problem with bool of another script but audio will not play yet D:

using UnityEngine;
using System.Collections;

public class AudioManager : MonoBehaviour {

    public AudioSource GameMusic;                            //Music playing while game is running
    public AudioSource PauseMusic;                           //Music playing while paused

    private GameObject _pause;                                  
    //Reference to the PauseMenu GameObject to get the paused bool out of it
    AudioSource[] audios = new AudioSource[2];



    // Use this for initialization
    void Awake ()
    {
        audios[0]= GameMusic;
        audios[1]= PauseMusic;
    }

    // Update is called once per frame
    void Update ()
    {
        if(_pause != null)
        {
            if (!_pause.GetComponent<PauseMenu>().paused)
            {
                audios[1].Stop();
                audios[0].Play();
            }
            else if (_pause.GetComponent<PauseMenu>().paused)
            {
                audios[0].Stop();
                audios[1].Play();
            }
        }
        else
        {
            _pause = GameObject.FindGameObjectWithTag("Pause");
        }
             
	}
}

The other Script is named “PauseMenu”.

I sitll have a problem when trying to play the musc D:

Hope you guys can help me out because I am stuck D:

Hi. Your problem is that you’re instructing your script to continuously start playing the active song every frame.

Under " if(_pause != null)" if paused is false or true, then the following statements will be executed every frame. By executing audios[num].Play() over and over again, it restarts the audio every frame so that you will never hear it because it never has a chance to play. Instead of playing the audio every frame, you should only set the target audiosource to play if it is not already playing by checking for AudioSource.isPlaying.

I went ahead and wrote a script for you to check out. It runs perfectly in my project and alternates songs based on the PauseMenu.pause property.[57173-audiomanager.txt|57173]

Hi,
The problem you are facing is that you have placed your code in update where either pause bool is true or false and stays true /false for some time.
As we know update function runs continuously , it keeps asking your method to play /stop sound. And thats why you are not able to hear anything.

So make sure you run your code once. Try something this:
using UnityEngine;
using System.Collections;

public class abc : MonoBehaviour {

		
	public PauseMenu  PauseMenuSCript_ref;
	public bool runonce;
	     
		public AudioSource[] audios = new AudioSource[2];
		
		
		
		// Update is called once per frame
		void Update ()
		{
			
		if (!PauseMenuSCript_ref.paused && !runonce)
			{
				print ("paused is false");
					audios[1].Stop();
					audios[0].Play();
			runonce = true;
			}
		else if (PauseMenuSCript_ref.paused && !runonce)
			{
				print ("paused is true");
					audios[0].Stop();
					audios[1].Play();
			runonce = true;
			}
			
			
			
		}
	}

And dont forget to make runonce bool false in pause menu script where you are changing your pause bool.
Hope I made my point clear.
Good Luck!!