Search Unity

My volume doesnt save across scenes with player prefs

Discussion in 'Scripting' started by shabbacreates, Mar 13, 2019.

  1. shabbacreates

    shabbacreates

    Joined:
    Jan 9, 2019
    Posts:
    7
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;


    public class MusicManager : MonoBehaviour
    {
    public Slider music;
    public Slider fxVolume;
    public AudioSource MusicSource;

    // Start is called before the first frame update
    void Start()
    {

    music.value = PlayerPrefs.GetFloat("MusicVolume");
    fxVolume.value = PlayerPrefs.GetFloat("FxVolume");
    }

    // Update is called once per frame
    void Update()
    {
    MusicSource.volume = music.value;
    }

    public void VolumePrefs()
    {

    PlayerPrefs.SetFloat("MusicVolume", (int)MusicSource.volume);
    PlayerPrefs.SetFloat("FxVolume", (int)fxVolume.value);
    PlayerPrefs.Save();

    }
    }
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,635
    I see several potential problems:
    Why are you casting the fxVolume.value as an int? Audio source volume is supposed to be float between 0 and 1, so if you cast it to an int, than will probably be truncated to 0 for almost any value you put in there.
     
    SparrowGS likes this.
  3. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Not even gonna read the code, use the tags as shown in the pinned thread.
     
    lordofduct likes this.
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    As was stated, use code tags:
    https://forum.unity.com/threads/using-code-tags-properly.143875/

    Furthermore, ask your question in your body of you post along with the code.

    The forum is full of people, so treat us like people.

    What if you someone walked up to you and said:
    "My invention spins backwards when using pliers"

    And then foisted a napkin with hard to read scribbles all over it.

    ...

    Yeah, that's effectively what you did. Sure you put your question in the title, but it's a poorly worded question with no details and uses words with multiple meanings (volume? sound volume? geometric volume? oh I see 'MusicManager scribbled on your napkin... I guess maybe sound?)

    And yes unformatted code is like a napkin with scribbles. Look at your post... can you read that? What makes you think I can?

    Sure I can make assumptions... but why should I?

    Who are you?

    Why do you think it's ok to ask people for help in this manner?

    Why should we put more effort into answering your question than you put into asking your question?

    (note this statement may seem like I'm singling just OP out... which I am sort of, but this goes for really 50%+ of the posts on this and other forums... I'm just in an extra ranty mood today).

    ...

    TLDR;

    As my father always said to my rude friends:
    "Should I go beat your parents for failing to raise you right?"
     
    Last edited: Mar 13, 2019
    SparrowGS, shabbacreates and eses like this.
  5. shabbacreates

    shabbacreates

    Joined:
    Jan 9, 2019
    Posts:
    7
    I apologise. Thank you all for helping and for the lesson. I am new to forums so I didnt know the necessary nomenclature.
    Thanks again
     
    lordofduct likes this.
  6. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    PlayerPrefs should be where you store and retrieve the volume between entire game sessions, but not within the game between scenes. You're basically writing to disk and then retrieving from disk for data already within your program. Makes no sense. Just pass volume between scenes using a GameObject with DontDestroyOnLoad set (lots of other ways, but that's a common one). There's lots of threads on the topic, so shouldn't have any issue finding how to use that.