Cannot convert type (class) to the same type (class)
SUMMARIZING MY MAIN QUESTION: "In my Save/Load script, how the heck can I define and initialize a class called GameSettings, to be exactly like the GameSettings (class) located in my GameControllerScript.cs
Lets say I have my main GameControllerScript.cs Inside of it, is a large class called GameSettings; and inside of the class GameSettings, are a couple other classes like: AudioSettings and VideoSettings.
GameControllerScripts.cs
[System.Serializable]
public class GameSettings
{
[System.Serializable]
public class PlayerControls
{
public bool controlIsKeyboard = true;
public int controlNumber = 1;
}
public PlayerControls[] _playerControls = new PlayerControls[4];
[System.Serializable]
public class PlayerKeys
{
public string horizontalKey = "";
public string verticalKey = "";
public string aButton = "";
public string bButton = "";
public string xButton = "";
public string yButton = "";
}
public PlayerKeys[] _playerKeys;
[System.Serializable]
public class AudioSettings
{
public bool soundEnbaled = true;
public float soundVolume = 1f;
}
public AudioSettings _audioSettings;
public class VideoSettings
{
public bool enabledAmbientOcclusion = true;
public bool enabledMSAA = true;
}
public VideoSettings _videoSettings;
}
public GameSettings _gameSettings;
OKAY, Now I have a SAVE/LOAD script that needs to save that entire GameSettings class. (FYI: I am following the persisting data tutorial on Unity)
SaveSettingsScript.cs
public GameControllerScript _controllerScript;
public class GameSettings { }
// Use this for initialization
void Start()
{
GameSettings _gameSettings = ????????????
}
AND THE SAVE FUNCTION (in the same script)
public void Save()
{
//the thing that savaes for us
BinaryFormatter bf = new BinaryFormatter();
//file creating
FileStream file = File.Create(Application.persistentDataPath + "/settingsInfo.dat");
// What data we are gonna put in here
// we need a clean class, NO MONOBEHAVIORS
GameSettings data = _gameControllerScript._gameSettings; // ??? I should be able to to make the variable data to an exact copy of class:GameSettings _gameSettings inside of _gameController??
//data.health = 10f; // or my local health
//data.experience = 0f;
bf.Serialize(file, data);
file.Close();
}
Obviously, I could go into the SaveSettingScript.cs and literally define the class:GameSettings to have all the sub-class definitions inside it...
but, isn't there like a more simple way to do this? something like...
this.gameObject._gameSettings = _gameControllerScript._gameSettings.GetType();
Answer by karma0413 · Dec 13, 2017 at 03:40 AM
Pretty Sure this fixed it Basically, the answer is here on line 10: You can define a class by referencing the Monobehavior class First and then the sub-class after it. Just as you would with variables, you can reference entire classes this same way.
Thank goodness I kept plugging away at it.
public void Save()
{
//the thing that savaes for us
BinaryFormatter bf = new BinaryFormatter();
//file creating
FileStream file = File.Create(Application.persistentDataPath + "/settingsInfo.dat");
// What data we are gonna put in here
// we need a clean class, NO MONOBEHAVIORS
GameControllerScript.GameSettings data = _controllerScript._gameSettings;
//data.health = 10f; // or my local health
//data.experience = 0f;
bf.Serialize(file, data);
file.Close();
}