- Home /
 
saving volume between scenes
I have a menu with a slider set up so I can adjust the volume. Although it works for my volume screen scene, it doesn't work in any other scene. I think I need to store the hsliderValue but I'm not even sure TBH. would really appreciate an assist.
 var backButton : Texture2D;
 var backRollover : Texture2D;
 var mouseEnterSound : AudioClip; 
 var mouseExitSound : AudioClip; 
 var hSliderValue : float = 0.5;
 private var correctedMousePosition : Vector2;
 private var mouseOver = false; // current mouse status
 private var isOver = false; // used to check status change
 
 
 function CheckButton(r: Rect, texOver: Texture2D, texOut: Texture2D): boolean {
     if ( r.Contains(correctedMousePosition) ) // if mouse over the button...
     {
         GUI.DrawTexture( r, texOver ); // draw the over texture...
         isOver = true;  // set isOver flag...
         return Input.GetMouseButtonUp(0); // and return mouse up status
     }
     else
     {
         GUI.DrawTexture( r, texOut ); // otherwise draw regular texture...
          return false; // and return false
     }
 }
 
 function OnGUI ()
 {
     correctedMousePosition = Vector2 (Input.mousePosition.x, (Screen.height-Input.mousePosition.y) );
     isOver = false; // prepare mouse enter/exit detection
     
     hSliderValue = GUI.HorizontalSlider(Rect(300,180,200,20), hSliderValue, 0.0, 1.0);
     GUI.Label(Rect(300,195,200,20), "Music="+hSliderValue);
     audio.volume = hSliderValue;
     
     if ( CheckButton( Rect(250,250,200,50), backRollover, backButton) )
     {
         Application.LoadLevel(1);
     }
     if (isOver != mouseOver){ // if mouse entered or exited button...
         mouseOver = isOver;   // update mouseOver and play appropriate sound
         if (mouseOver){
             audio.PlayOneShot(mouseEnterSound);
         } else {
             audio.PlayOneShot(mouseExitSound);
         }  
     }
 }
 
              Answer by Sundar · Sep 28, 2012 at 04:30 PM
You need a script that stay alive through out game play say "PlayerSettings"
Inside "PlayerSettings" call DontDestroyOnLoad(this) in Start() or Awake()
add volume variable to store its value from slider and set its value.
attach "PlayerSettings" to an empty gameobject
Answer by rokyed · Sep 29, 2012 at 07:21 AM
or you can use this :
PlayerPrefs.SetInt("volume", 10); // this is an int
PlayerPrefs.SetFloat("volume", 10.0); // this is a float
// and then just call it like this :
myScriptVolume =PlayerPrefs.GetInt("volume"); //to get the int
myScriptVolume =PlayerPrefs.GetFloat("volume"); // to get the float
// note this will save also the value if you close the unity game , and store it forever untill you vant to erase it if you want and here is the code:
PlayerPrefs.DeleteKey("volume");
or just delete all
PlayerPrefs.DeleteAll();
Your answer