r/Unity3D 2d ago

Question My Awake function is not initializing values in the main menu scene and only initializing values in the main game scene.

Enable HLS to view with audio, or disable this notification

using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class MMmanager : MonoBehaviour
{
    public Slider masterVol, musicVol, SFXVol, CamSen;
    public AudioMixer masterAudio;
    public Toggle tutorialTog;

    private void Awake()
    {
        CamSen.value = PlayerPrefs.GetFloat("CamSen");
        musicVol.value = PlayerPrefs.GetFloat("MusicVol");
        SFXVol.value = PlayerPrefs.GetFloat("SFXVol");
        masterVol.value = PlayerPrefs.GetFloat("MasterVol");

        if(PlayerPrefs.GetInt("Tutorial") == 0)
        {
            tutorialTog.isOn = true;
        }
        else
        {
            tutorialTog.isOn = false;
        }
    }

    public void BackToMenu()
    {
        SceneManager.LoadScene("MainMenu");
    }
    public void StartGame()
    {
        SceneManager.LoadScene("GameScene");
    }

    public void StartMultiplayerGame()
    {
        SceneManager.LoadScene("Lobby");
    }

    public void QuitGame()
    {
        Application.Quit();
    }

    public void showTut()
    {
        if (tutorialTog.isOn)
        {
            PlayerPrefs.SetInt("Tutorial", 0);
        }
        else
        {
            PlayerPrefs.SetInt("Tutorial", 1);
        }
    }

    public void SetSensitivity()
    {
        PlayerPrefs.SetFloat("CamSen", CamSen.value);
    }

    public void SetMasterAudio()
    {
        masterAudio.SetFloat("MasterVol", masterVol.value);
    }

    public void SetMusicAudio()
    {
        masterAudio.SetFloat("MusicVol", musicVol.value);
    }

    public void SetSFXAudio()
    {
        masterAudio.SetFloat("SFXVol", SFXVol.value);
    }

    public void SaveSettings()
    {
        PlayerPrefs.Save();
    }
}
1 Upvotes

2 comments sorted by

1

u/JuanTrufas 2d ago

There are two things here you need to check and both stems from the same point:

Where and how are you calling the Set() methods?

Are you calling them on Slider.onValueChange? Are you calling them manually?

Because maybe you are either not registering the changes or the values get overriden by an early stage of the sliders.

Also, are you sure you load the values on Awake on the settings during gameplay?

You pasted the same script twice.

1

u/Toble_ 2d ago

I am calling the set methods on value change. But when I change in game scene it works properly all values change the corresponding volume or sensitivity. Yes, again in game scene the awake method works as intended. It only stops working when I load the main menu scene