I am currently working on 2D platform game. The following script is supposed to disable the audio whenever the toggle button is off.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using System.IO;
public class SettingsManager : MonoBehaviour {
// Use this for initialization
public Toggle audioEnabled;
public Button applyButton;
public GameSettings gameSettings;
void OnEnable()
{
gameSettings = new GameSettings();
audioEnabled.onValueChanged.AddListener(delegate{AudioDisabledToggle();});
Debug.Log(GameSettings.index);
applyButton.onClick.AddListener(delegate{OnApplyButtonClick();});
LoadSettings();
}
public void AudioDisabledToggle()
{
Debug.Log(gameSettings.audioEnabled);
gameSettings.audioEnabled = AudioListener.pause = !audioEnabled.isOn;
}
public void OnApplyButtonClick()
{
SaveSettings();
foreach (GameObject gameObject in GameObject.FindGameObjectsWithTag("Settings"))
{
GetComponent<AudioSource>().Play();
gameObject.SetActive(false);
}
}
public void SaveSettings()
{
string jsonData = JsonUtility.ToJson(gameSettings, true);
File.WriteAllText(Application.persistentDataPath + "/gameSettings.json", jsonData);
Debug.Log("Saved");
}
public void LoadSettings()
{
File.ReadAllText(Application.persistentDataPath + "/gameSettings.json");
gameSettings = JsonUtility.FromJson<GameSettings>(File.ReadAllText(Application.persistentDataPath + "/gameSettings.json"));
Debug.Log("Loaded");
audioEnabled.isOn = !gameSettings.audioEnabled;
//Debug.Log("Loaded");
}
}
These script are working before but recently I get this error
NullReferenceException: Object reference not set to an instance of an object
There's a problem with these lines audioEnabled.isOn = !gameSettings.audioEnabled;
and AudioListener.pause = gameSettings.audioEnabled = !audioEnabled.isOn;
and I found out that there's a problem with my audioEnabled in this class
public class GameSettings{
// Use this for initialization
public bool audioEnabled;
public static bool index;
}
I don't know what's the problem is because when I use this scene with these scripts that I have exported inside another project it worked, except the current project that I've been working on right now. Please help.