Answer the question
In order to leave comments, you need to log in
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();
}
Answer the question
In order to leave comments, you need to log in
GetComponentInChildren() returned null - i.e. component "slider" was not found in childs.
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 questionAsk a Question
731 491 924 answers to any question