我目前正在开发2D平台游戏。每当切换按钮关闭时,以下脚本都应禁用音频。
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");
}
}
这些脚本以前工作但最近我收到此错误
NullReferenceException:未将对象引用设置为对象的实例
这些行audioEnabled.isOn = !gameSettings.audioEnabled;
和AudioListener.pause = gameSettings.audioEnabled = !audioEnabled.isOn;
存在问题,我发现此类中的audioEnabled存在问题
public class GameSettings{
// Use this for initialization
public bool audioEnabled;
public static bool index;
}
我不知道问题是什么,因为当我将这个场景与我在其他项目中导出的脚本一起使用时,除了我正在处理的当前项目马上。请帮忙。