D
D
Danil Afanasiev2017-06-15 16:48:59
C++ / C#
Danil Afanasiev, 2017-06-15 16:48:59

How to modify the script so that the sound level is the same on all scenes?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class settingsmenu : MonoBehaviour {
    
    public AudioSource Music;
    public Slider bar;
    

    public void Start()
    {
        
            Music.Play();

        

        
    }
    public void Update()
    {
        AudioListener.volume = bar.value;
        
    }


   
}

How to modify the script so that the sound level is the same on all scenes?
The game itself: https://vk.com/app6070955_209422110

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
Denis Gaydak, 2017-06-15
@danil_afan

Nevertheless, let's give separate advice, I will give a separate answer.
You now have sound settings strongly associated with the playback component itself.
For good, you have a static or global (or whatever you want) data structure for settings.
Alya some class where there is "volume", "additional effects", "selected skin" and so on. just data.
The settings menu will allow these data to be changed and saved.
And here's the playback script - access this data and use it.
for example, if you name a class with GlobalSettings settings))

public class GlobalSettings
{
 public static float musicVolume;
}

then from it we take the volume for the script where we control the music
public void Update()
    {
        AudioListener.volume = GlobalSettings.musicVolume;
        
    }

but changing the position of the slider will change our new global variable)
float newVolume = //тут как нибудь получаете новое значение 
// к примеру из эвента  ChangeValue() в слайдере..или еще как 
GlobalSettings.musicVolume = newVolume ;

I hope I conveyed the general principle and the meaning of how I would make the settings
(I wrote the code like this out of my head, typos and inaccuracies are possible, but here I conveyed the meaning and solution, and did not write the code for you)

O
Oleg Pogrebnyak, 2017-06-15
@S0HardCore

Pass volume between scenes with PlayerPrefs.SetFloat/PlayerPrefs.GetFloat.

D
Daniil Basmanov, 2017-06-15
@BasmanovDaniil

Read the Audio Mixer manual and change the volume not with AudioListener.volume , but with AudioMixer.SetFloat .

/
/ "BRO TIGER", 2017-06-15
@BRO_TIGER

Was it the same in all scenes? Isn't it easier to indicate that the sound level at the start of the program was equal to the level that you write? (I propose this idea)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question