-1
\$\begingroup\$

I have the ui slider to control my volume, I have my slider to control all the volume of the game. I have make it so when the main slider change value all the other slider change with it but when I try to use the other slider to change the volume of the game doesn't work, the audio only change when I use the main slider, I want that when you use the other slider it can control the volume of the game too but I don't know how. Can anyone help?

Code:

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

public class MainAudio : MonoBehaviour {

public Slider slider;

void Awake()
{
    if(slider){
    GetComponent<AudioSource> ().volume = PlayerPrefs.GetFloat ("CurVol");
    slider.value = GetComponent<AudioSource>().volume;
}
}
public void VolumeControl(float volumeControl)
{
    GetComponent<AudioSource> ().volume = volumeControl;
    PlayerPrefs.SetFloat ("CurVol", GetComponent<AudioSource> ().volume);
    PlayerPrefs.Save ();
}
private void Update()
{
    VolumeControl(slider.value);
}
void OnApplicationQuit() {
    PlayerPrefs.Save();
}
}
\$\endgroup\$
  • \$\begingroup\$ Please dont formulate long sentences when you dont know the technical terms. Try to restructure the sentences. \$\endgroup\$ – Doctor Niklas Jul 6 '15 at 12:14
0
\$\begingroup\$

I answered a question from you on this source code a few days ago and already saw that you dont really think about what you code.

At first -as I said you-, remove the PlayerPrefs lines from the VolumeControl method, this is producing unneeded overhead, because you update and save the "CurVol" value from the Registry in the OnApplicationQuit method.

private void OnApplicationQuit()
{
    PlayerPrefs.SetFloat("CurVol", GetComponent<AudioSource> ().volume);
    PlayerPrefs.Save();
}
public void VolumeControl(float volumeControl)
{
    GetComponent<AudioSource>().volume = volumeControl;
}

And i also cannot help you with your current problem because you did not update your source code to what you have or at least tried. There is no reference to other sliders and not even the slightest change from last time where I answered your question. Please try to put a little bit more effort into your problem description, and maybe another guy in the Internet had the same problem as you sometime before and gave an answer in a forum. Google could be a good start here.

\$\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.