0
\$\begingroup\$

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

\$\endgroup\$
1
\$\begingroup\$

The error is telling you that, you're trying to access a non-static variable volume with the class name AudioSource, which is not possible. To access a non-static member, you neeed an Instance of the class.

Therefore, you have to define an Instance of the AudioSource class to access volume variable, and then assign it an AudioSource component either from Inspector or from code. Like this :

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class AudioManager : MonoBehaviour {
    public Button btnOn;
    public Button btnOff;

    public AudioSource audioSource; 

    // Use this for initialization
    void Start () {
        btnOn = GetComponent<Button>();
        btnOff = GetComponent<Button>();

        // Either use this, or assign the component from inspector
        audioSource = GetComponent<AudioSource>(); 

        btnOn.onClick.AddListener(() => PlayAudio());
        btnOff.onClick.AddListener(() => StopAudio());
    }

    void PlayAudio()
    {
        audioSource.volume = 0.5f;
    }

    void StopAudio()
    {
        audioSource.volume = 0f;
    }
} 
\$\endgroup\$
  • \$\begingroup\$ I have explained in my question that, if I use public AudioSource audioSource, I have to add another bgm to GetComponent. So, I will have two GetComponent, is that okay to have two? \$\endgroup\$ – Grace Michelle Dec 15 '16 at 7:21
  • 1
    \$\begingroup\$ @GraceMichelle, I clearly didn't get what you meant by saying I will have two GetComponent, because GetComponent is just a generic method you can use as many times as you like. \$\endgroup\$ – Aditya Yadav Dec 15 '16 at 12:21
0
\$\begingroup\$

I think you're confusing AudioListener with AudioSource. They're different things. There can be many audio sources in the scene but only one audio listener. Try using AudioListener.volume to set the volume instead.

You might also want to pause it.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.