Unity Volume Slider Scenes Problem

Hello guys, got a question. I added music to my block breaker game and while it does go through every scene and plays perfectly it doesn’t use the slider properly, if the player plays the game and comes back to the menu then he can’t change the slide again to increase or decrease volume and they remain with music or no music depending on their choice at the start of the game. Code is this. Any suggestions?

FIRST SCRIPT VOLUME VALUE CHANGER

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UIElements;

using UnityEngine.Audio;

public class VolumeValueChange : MonoBehaviour
{
    float volume = 0.5f;
    private AudioSource AudioSrc;
    private float AudioVolume = 1f;

    void Start () {
        AudioSrc = GetComponent<AudioSource>();
        volume = PlayerPrefs.GetFloat("SliderVolumeLevel", volume);
    }

    void Update () {
        AudioSrc.volume = AudioVolume;
        PlayerPrefs.SetFloat("SliderVolumeLevel", volume);
    }

    public void SetVolume(float vol)
    {
        AudioVolume = vol;
        PlayerPrefs.SetFloat("SliderVolumeLevel", volume);
        AudioListener.volume = volume;
    }
}

SECOND SCRIPT SOUNDMANAGER

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;

public class SoundManager : MonoBehaviour
{    
    private static SoundManager instance = null;
    public static SoundManager Instance
    {
        get { return instance; }
    }

    void Awake()
    {
        if (instance != null && instance != this)
        {
            Destroy(this.gameObject);
            return;
        }
        else
        {
            instance = this;
        }

        DontDestroyOnLoad(this.gameObject);
    }
}

Hi @Sake_Jr,

Welcome to our community! :slight_smile:

Please note, it’s better to copy/paste your code and apply the code fencing characters, rather than using screenshots. Screenshots are ideal for displaying specific details from within a game engine editor or even error messages, but for code, they tend to be less readable, especially on mobile devices which can require extensive zooming and scrolling.

You also prevent those that may offer to help you the ability to copy/paste part of your code back to you with suggestions and/or corrections, meaning that they would need to type a potentially lengthy response. You will often find that people are more likely to respond to your questions if you make it as easy as possible for them to do so.

Are there any error messages in your console when the slider stops working properly? Have you already tried to add Debug.Logs to your code to see what is going on during runtime?

Hope this helps :slight_smile:


See also;

First of all thank you for taking the time to inform me about that, i edited my post in order to make it easier. Secondly there’s no error when using Debug.Log with this code so i’m pretty much clueless on what to do next in order to solve this. Tried couple methods but they tend to be way worse than this one. Thanks again!

Thanks a lot for sharing your code as text. That’s much better. I formatted it for you, so it is easier to read for me. :slight_smile:

Debug.Logs are not supposed to throw any errors. They are supposed to tell you what is going on during runtime. You could make an assumption such as “this is working”, and use a Debug.Log to verify that is it indeed working.

Add a Debug.Log to the SetVolume method to see if it gets called. If the VolumeValueChange component is part of the game object or the children to which the SoundManager component is attached, it might be that the slider loses the reference to the SoundManager component when a new scene gets loaded. The best would be to take a look at the Inspector when the VolumeValueChange component is working as expected and when it is not working as expected. Maybe you will be able to spot a difference.

Generally, if you do not know where the problem might be, just look “everywhere” until you find something unexpected. :slight_smile:

Thanks again for replying. I tried debug.logging SetVolume and it says i miss a reference which sounds exactly like what you said. Problem is, my knowledge level at the moment is less than required to fix that…Hahahaha i’m trying stuff out, hopefully i’ll fix it but it’s not easy for my current self…
image
Also every time i change volume i get the debug.log on vol, but if i RE-ENTER the scene then i get zero debug.log even if i increase or decrease volume.

The error with “Unknown” is often very easy to fix. Sometimes, when you rename or delete a script, Unity loses the connection to that script. In that case, check the Inspector of your game objects until you find a “missing” component in the Inspector. Remove that component if you do not need it. Replace it with a new component if you need it. Solve that problem first. If you are lucky, your other problem depends on this one and automatically fixes itself after you solved this one.

Sorry for the late reply, “Life” hit me recently. I haven’t figured a way to fix it but i’ll keep trying in the future while working on other projects as well. Thank you for your suggestions!

This topic was automatically closed after 6 days. New replies are no longer allowed.

Privacy & Terms