- Home /
Can't get Json out of class
Hello everyone!
It seems that I can't get my JSON values out of my class... Here's the script code: using System.Collections; using System.Collections.Generic; using UnityEngine;
using System.IO;
public class SaveAndLoad : MonoBehaviour {
public int InstancesCreated;
public bool Loading = false;
public bool Startup;
public int LoadingObjID;
void Start()
{
if (File.Exists(Application.dataPath + "\\Games\\" + PlayerPrefs.GetString("Game") + "\\gamevars.json") == true)
{
JsonUtility.FromJson<GameData>(File.ReadAllText(Application.dataPath + "\\Games\\" + PlayerPrefs.GetString("Game") + "\\gamevars.json"));
if (GetComponent<GameData>().Startup == false)
{
LoadGame();
}
}
}
public void SaveGame()
{
//Variables needed
InstancesCreated = GameObject.Find("ObjectSelectWindow").GetComponent<CreateObjectButton>().InsCreated;
//Save Objects
//Checks if all directories exist and creates them if they don't
if (Directory.Exists(Application.dataPath + "\\Games") == false)
{
Directory.CreateDirectory(Application.dataPath + "\\Games");
}
if (Directory.Exists(Application.dataPath + "\\Games\\" + PlayerPrefs.GetString("Game")) == false)
{
Directory.CreateDirectory(Application.dataPath + "\\Games\\" + PlayerPrefs.GetString("Game"));
}
if (Directory.Exists(Application.dataPath + "\\Games\\" + PlayerPrefs.GetString("Game") + "\\Objects") == false)
{
Directory.CreateDirectory(Application.dataPath + "\\Games\\" + PlayerPrefs.GetString("Game") + "\\Objects");
}
//Delectes all object files in *\Objects\
var Files = Directory.GetFiles(Application.dataPath + "\\Games\\" + PlayerPrefs.GetString("Game") + "\\Objects");
for (int i = 1; i < Files.Length; i++)
{
File.Delete(Files[i]);
}
//Saves objects into *\Objects\
for (int i = 1; i < InstancesCreated + 1; i++)
{
GameObject.Find("Object_" + i).GetComponent<ObjectScript>().Save();
File.WriteAllText(Application.dataPath + "\\Games\\" + PlayerPrefs.GetString("Game") + "\\Objects\\" + i + ".json", JsonUtility.ToJson(GameObject.Find("Object_" + i).GetComponent<ObjectScript>()));
}
//Save Game Variables (Instances Created)
File.WriteAllText(Application.dataPath + "\\Games\\" + PlayerPrefs.GetString("Game") + "\\gamevars.json", JsonUtility.ToJson(gameObject.GetComponent<SaveAndLoad>()));
}
public void LoadGame()
{
Loading = true;
//Loads GameData
JsonUtility.FromJson<GameData>(File.ReadAllText(Application.dataPath + "\\Games\\" + PlayerPrefs.GetString("Game") + "\\gamevars.json"));
InstancesCreated = gameObject.GetComponent<GameData>().InstancesCreated;
GameObject.Find("ObjectSelectWindow").GetComponent<CreateObjectButton>().InsCreated = InstancesCreated;
//Loads Objects
GameObject CubePref = GameObject.Find("ObjectSelectWindow").GetComponent<CreateObjectButton>().CubePref;
Transform SpawnPosEmpty = GameObject.Find("ObjectSelectWindow").GetComponent<CreateObjectButton>().SpawnPosEmpty;
for (int i = 0; i < InstancesCreated + 1; i++)
{
JsonUtility.FromJson<ObjectData>(File.ReadAllText(Application.dataPath + "\\Games\\" + PlayerPrefs.GetString("Game") + "\\Objects\\" + i + ".json"));
string ObjType = gameObject.GetComponent<ObjectData>().Type;
LoadingObjID = i;
if (ObjType == "Cube")
{
Instantiate(CubePref, new Vector3(SpawnPosEmpty.transform.position.x, SpawnPosEmpty.transform.position.y, SpawnPosEmpty.transform.position.z), new Quaternion(0, 0, 0, 1));
}
}
Loading = false;
}
}
[System.Serializable]
public class GameData
{
public int InstancesCreated;
public bool Loading;
public bool Startup;
public int LoadingObjID;
}
[System.Serializable]
public class ObjectData
{
public float[] Position;
public string Type;
}
Thanks! Oh, and the error comes up with this:
ArgumentException: GetComponent requires that the requested component 'GameData' derives from MonoBehaviour or Component or is an interface. UnityEngine.Component.GetComponent[GameData] () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/ComponentBindings.gen.cs:48) SaveAndLoad.Start () (at Assets/_Scripts/GameMaker/SaveAndLoad.cs:21)
Comment
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
JSON value assignment 0 Answers
How to protect JSON file game data? 3 Answers