Changing the volume of an audio source

I am trying to figure out how I can change the game volume using my slider… but as the previous tip says, there is an audio source added… I just created an empty object and put a audio source inside…

1: Where should I place my audio source?
2: If I put another audio source (with my load sound) in my scene, the load sound get played twice and explodes my ears and my headset.
3: How can I set the audio source on my LevelManager, I can’t have 2 objects at the sane time selected so I can drag my Audio Source. Also I can’t select it from my assets. (> NullReferenceException: Object reference not set to an instance of an object

LevelManager.ChangeVolume (Single volume) (at Assets/Scripts/LevelManager.cs:45)
SliderController.Update () (at Assets/Scripts/SliderController.cs:22)
)

Please help me!

Also, here are my scripts:

SliderController.cs:

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

public class SliderController : MonoBehaviour {

	public Slider volumeSlider;
	public LevelManager levelManager;

	private MusicManager musicManager;

	// Use this for initialization
	void Start () {
		musicManager = GameObject.FindObjectOfType<MusicManager>();

		volumeSlider.value = PlayerPrefsManager.GetMasterVolume ();

	}
	
	// Update is called once per frame
	void Update () {
		levelManager.ChangeVolume (volumeSlider.value);
	}

	public void SaveAndExit()
	{

		PlayerPrefsManager.SetMasterVolume (volumeSlider.value);
		levelManager.LoadLevel("01a Start");

	}
}

LevelManager.cs:

using UnityEngine;
using System.Collections;

public class LevelManager : MonoBehaviour {

	public float autoLoadNextLevelAfter;

	private AudioSource audioSource;

	void Start()
	{
		if (autoLoadNextLevelAfter == 0) {

			Debug.Log ("Level autoload disabled");

		} else {

			Invoke("LoadNextLevel", autoLoadNextLevelAfter);

		}

		audioSource = GameObject.Find ("Load Sound").GetComponent<AudioSource> ();
	}

	public void LoadLevel(string name){
		Debug.Log ("New Level load: " + name);
		Application.LoadLevel (name);
	}

	public void QuitRequest(){
		Debug.Log ("Quit requested");
		Application.Quit ();
	}

	public void LoadNextLevel()
	{

		Application.LoadLevel (Application.loadedLevel + 1);

	}

	public void ChangeVolume(float volume)
	{

		audioSource.volume = volume;

	}

}

Ok, I figured it out… I wasn’t searching for the right object… but I am getting another error when I enter the scene with the volume slide (options menu), it says now: > NullReferenceException: Object reference not set to an instance of an object

LevelManager.ChangeVolume (Single volume) (at Assets/Scripts/LevelManager.cs:45)
SliderController.Update () (at Assets/Scripts/SliderController.cs:22)

Some help please?

I figured this out too (not rly…), I actually managed to make the slider change the master volume, and not the music… I will later figure out how to change the music volume… thanks for the help (nobody).

From memory, in this section, there is only master volume, it is for the one audio source which is used to play the music.

If you wanted to add some sound effects for the game, perhaps when projectiles are launched or make an impact, you could add a secondary slider for “Effects Volume”, perhaps rename the original to “Music Volume” etc. Then, when you play an audio clip for an effect you could use;

AudioSource.PlayOneShot(audioClip, effectsVolume);

In the above, audioClip would be the audio clip for the specific effect you want to play, effectsVolume would be the value from your slider, stored in a variable, which is then used for every sound effect.

You could also use PlayClipAtPoint as per BlockBreaker.

Also, when you copy/paste your code, it’s always best to apply the code formatting so that it is more readable for other people, see below.


See also;

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms