[Error:] Cannot Deserialize JSON to new instances of type ' X '
Hello! I have a menu of options and I want to save the settings, but I get the error that I leave you in a capture
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
public class SettingManager : MonoBehaviour {
public Toggle fullscreenToggle;
public Dropdown resolutionDropdown;
public Dropdown textureQualityDropdown;
public Dropdown antialiasingDropdown;
public Dropdown vSyncDropdown;
public Slider musicVolumeSlider;
public Button applyButton;
public Button exitButton;
public Button cancelButton;
public AudioSource musicSource;
public Resolution[] resolutions;
public GameSettings gameSettings;
void OnEnable()
{
gameSettings = new GameSettings();
fullscreenToggle.onValueChanged.AddListener(delegate { OnFullscreenToggle(); });
resolutionDropdown.onValueChanged.AddListener(delegate { OnResolutionChange(); });
textureQualityDropdown.onValueChanged.AddListener(delegate { OnTextureQualityChange(); });
antialiasingDropdown.onValueChanged.AddListener(delegate { OnAntialiaingChange(); });
vSyncDropdown.onValueChanged.AddListener(delegate { OnVSyncChange(); });
musicVolumeSlider.onValueChanged.AddListener(delegate { OnMusicVolumeChange(); });
applyButton.onClick.AddListener (delegate { OnApplyButtonClick(); });
exitButton.onClick.AddListener (delegate { OnExitButtonClick(); });
cancelButton.onClick.AddListener (delegate { OnCancelButtonClick(); });
resolutions = Screen.resolutions;
foreach(Resolution resolution in resolutions)
{
resolutionDropdown.options.Add(new Dropdown.OptionData(resolution.ToString()));
}
LoadSettings();
}
public void OnFullscreenToggle()
{
gameSettings.fullscreen = Screen.fullScreen = fullscreenToggle.isOn;
}
public void OnResolutionChange()
{
Screen.SetResolution(resolutions[resolutionDropdown.value].width, resolutions[resolutionDropdown.value].height, Screen.fullScreen);
}
public void OnTextureQualityChange()
{
QualitySettings.masterTextureLimit = gameSettings.textureQuality = textureQualityDropdown.value;
}
public void OnAntialiaingChange()
{
QualitySettings.antiAliasing = gameSettings.antialiasing = (int)Mathf.Pow(2f, antialiasingDropdown.value);
}
public void OnVSyncChange()
{
QualitySettings.vSyncCount = gameSettings.vSync = vSyncDropdown.value;
}
public void OnMusicVolumeChange()
{
musicSource.volume = gameSettings.musicVolume = musicVolumeSlider.value;
}
public void OnApplyButtonClick()
{
SaveSettings();
}
public void OnCancelButtonClick()
{
LoadSettings();
}
public void OnExitButtonClick()
{
Application.Quit();
}
public void SaveSettings()
{
string jsonData = JsonUtility.ToJson(gameSettings, true);
File.WriteAllText(Application.persistentDataPath + "/gamesettings.json", jsonData);
}
public void LoadSettings()
{
gameSettings = JsonUtility.FromJson<GameSettings>(File.ReadAllText(Application.persistentDataPath + "/gamesettings.json"));
musicVolumeSlider.value = gameSettings.musicVolume;
antialiasingDropdown.value = gameSettings.antialiasing;
vSyncDropdown.value = gameSettings.vSync;
textureQualityDropdown.value = gameSettings.textureQuality;
resolutionDropdown.value = gameSettings.resolutionIndex;
fullscreenToggle.isOn = gameSettings.fullscreen;
}
}
captura.png
(11.0 kB)
Comment
Answer by Dima129B · Oct 25, 2017 at 04:31 PM
You load every time the game open, but when it's the first time the player won't have any gamesettings.json and then there is an error, I solved this problem by adding an if statement before loading the game settings :
if (File.Exists(Application.persistentDataPath + "/gamesettings.json") == true)
{
LoadSettings();
}
It just check if there is already a gamesettings.json, if not it will just skip the loading until the player decide to apply in the option menu and restart the game I think.