D
D
Denchik1020302021-11-23 22:54:10
C++ / C#
Denchik102030, 2021-11-23 22:54:10

Why does it throw NullReferenceException: Object reference not set to an instance of an object?

Can you tell me why it gives me an error:
NullReferenceException: Object reference not set to an instance of an object
SettingsButtons.Start () (at Assets/Settings/Scipts/SettingsButtons.cs:14)
In the code (if anything, line 14 is GetComponentInChildren().value = PlayerPrefs.GetFloat("SoundVolume", 1);):

public class SettingsButtons : MonoBehaviour
{

    [SerializeField] private AudioSource _buttonClickAudio;
    [SerializeField] private AudioMixerGroup _mixer;

    private void Start()
    {
        GetComponentInChildren<Slider>().value = PlayerPrefs.GetFloat("SoundVolume", 1);
    }

    private void GoToMenu()
    {
        SceneManager.LoadScene("MainMenu");
    }

    public void OnMenuButtonClick()
    {
        _buttonClickAudio.Play();
        Invoke("GoToMenu", 1f);
    }

    public void ToggleSound(float volume)
    {
        _mixer.audioMixer.SetFloat("ButtonSoundToggle", Mathf.Lerp(-80, 0, volume));
        PlayerPrefs.SetFloat("SoundVolume", volume);
        PlayerPrefs.Save();
    }


Thanks in advance for your reply!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
GavriKos, 2021-11-23
@Denchik102030

GetComponentInChildren() returned null - i.e. component "slider" was not found in childs.

D
Denchik102030, 2021-11-24
@Denchik102030

Thank you! Helped me so much! But if the language is simpler (just in case for beginners).
I had a Panel, under which there was this same Slider, and I needed to attach the script to the Panel

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question