I have this script here which changes the volume in the main menu but how would I change the volume for the whole game not just the main menu scene. I am not new to unity but I am new to the new unity 5 UI and audio. So if anyone could help that would be great.
public void Volume(float newVolume)
{
AudioListener.volume = newVolume;
}
You can also use an Audio Mixer for this. It’s a bit of work to set it up, but once it’s done it’s a very powerful tool.
To change anything, you first have to expose the desired parameter to be able to manipulate it - this can be almost every value from the mixer including effects, etc.
After creating you mixer, you have to assign it to all AudioSources that shall be affected as Output
Open the Audio Mixer window and select your Audio Mixer
Select the group you want to manipulate ( e.g.Master for main volume)
In the Inspector right click on the label Volume
Click on Expose 'Volume (of Master)' to script
Rename the Exposed Parameter in the dropdown in the top right corner of the Audio Mixer window to e.g. MasterVolume
public class SetAudioParameter : MonoBehaviour
{
public AudioMixer mixer;
public string parameterName = "MasterVolume";
protected float Parameter
{
get
{
float parameter;
mixer.GetFloat(parameterName, out parameter);
return parameter;
}
set
{
mixer.SetFloat(parameterName, value);
}
}
}
Of course, you first have to drag in your AudioMixer to the Component’s field in the editor, to make it work.
Perhaps you could use the PlayerPrefs to store the newVolume float, since PlayerPrefs are saved between sessions (and, a fortiori, also between scenes!)
So, when you change the volume:
using UnityEngine;
using System.Collections;
public class Volume: MonoBehaviour
{
public void changeVolume(float newVolume)
{
PlayerPrefs.SetFloat("volume", newVolume);
AudioListener.volume = PlayerPrefs.GetFloat("volume");
}
}
On every scene, you can then add:
using UnityEngine;
using System.Collections;
public class GameController : MonoBehaviour
{
private void Start()
{
AudioListener.volume = PlayerPrefs.GetFloat("volume");
}
}
The video actually offers a very slick method of doing this, and uses a very simple single script that is public and attached to some canvas or gameobject so the functions are available publicly.
public class MixLevels : MonoBehaviour {
public AudioMixer masterMixer;
public void SetSfxLevel(float sfxLvl) {
masterMixer.SetFloat ("volSfx", sfxLvl);
}
public void SetMusicLevel(float musicLvl) {
masterMixer.SetFloat ("volMusic", musicLvl);
}
}
Then you just have two sliders, and they just call the functions directly from the inspector settings of the sliders:
This allows you to change two game volume options with just one script and two UI sliders.
Ebonicus you refer to a video... I have read the whole thread a couple of times and didn't see a link to a video. Which video? I tried the method you describe and so far no luck, so I would like to find that video. Unity is funny that way -- sometimes it seems insanely easy to do something that you would think was really complicated and difficult, other times it seems insanely difficult to do something that you would think would be simple and easy (like presenting the user with volume controls!)...
Ebonicus you refer to a video... I didn't see a link in the thread to a video. I would like to view it, because I tried the method you describe and have not got it to work yet.
You can do two things. If the motion is always the same, then just create an animation clip and set the keyframes accordingly (by changing the transform.position). If the characters should move more flexibly, then use Vector3.Lerp along with the transform.position and target position. The Scripting API has examples on how to use it.
Thanks didn't think of this will add this
– rodude123