0

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.

  • It's exactly as it tells you. You're referencing an object that isn't instantiated. It doesn't exist. – ThePerplexedOne Dec 01 '16 at 11:09
  • I gave the project file to my friend so he can open them in his computer, and it works, I've created new project and I had exported the whole project package from original project to the new project and it runs perfectly. I even open the same project file that I gave to my friend and the same error occurs. – user3132527 Dec 01 '16 at 12:35

0 Answers0