I'm working with Unity 5.3 with C# as code editor
These are what I have :
I have 2 scenes in my project : home
and options
. I have bg
object on both scenes. Both bg
objects have Audio Source
component, which contains same background music that play on awake
. I don't use any codes for these background musics, I only click the Add Component
button from Unity and add Audio Source
.
This is what I want :
Options
scene can toggle the background music on/off for all scenes. Therefore, there are btnOn
and btnOff
in Options
scene.
This is my code in Audio Manager.cs :
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class AudioManager : MonoBehaviour {
public Button btnOn;
public Button btnOff;
// Use this for initialization
void Start () {
btnOn = GetComponent<Button>();
btnOff = GetComponent<Button>();
btnOn.onClick.AddListener(() => PlayAudio());
btnOff.onClick.AddListener(() => StopAudio());
}
void PlayAudio()
{
AudioSource.volume = 0.5f;
}
void StopAudio()
{
AudioSource.volume = 0f;
}
}
This is the problem :
I have this error : An object reference is required to access non-static member UnityEngine.AudioSource.volume
. Maybe, this is because I don't write public AudioSource audioSource
in my code. But, if I write this, I have to add another audio in Get Component
box, and I will have double Audio Source
in one scene. What should I do? Thanks