Cannot deserialize JSON to new instances of type 'X'
I'm making a menu of options, and I want the settings made to be saved in .json format, that does not give any errors, but when I start the game and I want to load them, it says:
Can not deserialize JSON to new instances of type 'X'
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 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(); });
resolutions = Screen.resolutions;
foreach(Resolution resolution in resolutions)
{
resolutionDropdown.options.Add(new Dropdown.OptionData(resolution.ToString()));
}
if (File.Exists(Application.persistentDataPath + "/gamesettings.json") == true)
{
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 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;
}
}
@haruna9x |This script I've taken from the internet, and I do not understand what I should do, I've tried it but it gives me errors. Can you explain it a little better?
Sorry for my bad English, I'm from Spain.
@haruna9x | I'm really sorry if I bother you too much, but it's that you can not delay any more, it's the last thing that is loaded in the game, and I do not know I have to overwrite.
@haruna9x | I'm sorry if I bother you too much, but it's that you can not delay any more, it's the last thing that is loaded in the game, and I do not know I have to overwrite :(
Answer by Unplug · Jan 24, 2018 at 01:49 PM
I came across the same problem. You simply forgot to delete the monobehavior from gamesettings. open the file and make sure it read
public class GameSettings {
you probably have the standard ending " : Monobehavior { " but JSON cannot read monobehavior class and this is what it is trying to tell you. they only read class.