Search Unity

Can't get Json out of class

Discussion in 'Scripting' started by SeaPeaMe, Apr 21, 2018.

  1. SeaPeaMe

    SeaPeaMe

    Joined:
    Jan 23, 2018
    Posts:
    28
    Hello everyone!

    It seems that I can't get my JSON values out of my class... Here's the script code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. using System.IO;
    6.  
    7. public class SaveAndLoad : MonoBehaviour {
    8.  
    9.     public int InstancesCreated;
    10.     public bool Loading = false;
    11.     public bool Startup;
    12.  
    13.     public int LoadingObjID;
    14.  
    15.     void Start()
    16.     {
    17.         if (File.Exists(Application.dataPath + "\\Games\\" + PlayerPrefs.GetString("Game") + "\\gamevars.json") == true)
    18.         {
    19.             JsonUtility.FromJson<GameData>(File.ReadAllText(Application.dataPath + "\\Games\\" + PlayerPrefs.GetString("Game") + "\\gamevars.json"));
    20.  
    21.             if (GetComponent<GameData>().Startup == false)
    22.             {
    23.                 LoadGame();
    24.             }
    25.         }
    26.     }
    27.  
    28.     public void SaveGame()
    29.     {
    30.         //Variables needed
    31.         InstancesCreated = GameObject.Find("ObjectSelectWindow").GetComponent<CreateObjectButton>().InsCreated;
    32.  
    33.  
    34.         //Save Objects
    35.  
    36.         //Checks if all directories exist and creates them if they don't
    37.  
    38.         if (Directory.Exists(Application.dataPath + "\\Games") == false)
    39.         {
    40.             Directory.CreateDirectory(Application.dataPath + "\\Games");
    41.         }
    42.  
    43.         if (Directory.Exists(Application.dataPath + "\\Games\\" + PlayerPrefs.GetString("Game")) == false)
    44.         {
    45.             Directory.CreateDirectory(Application.dataPath + "\\Games\\" + PlayerPrefs.GetString("Game"));
    46.         }
    47.  
    48.         if (Directory.Exists(Application.dataPath + "\\Games\\" + PlayerPrefs.GetString("Game") + "\\Objects") == false)
    49.         {
    50.             Directory.CreateDirectory(Application.dataPath + "\\Games\\" + PlayerPrefs.GetString("Game") + "\\Objects");
    51.         }
    52.  
    53.         //Delectes all object files in *\Objects\
    54.         var Files = Directory.GetFiles(Application.dataPath + "\\Games\\" + PlayerPrefs.GetString("Game") + "\\Objects");
    55.         for (int i = 1; i < Files.Length; i++)
    56.         {
    57.             File.Delete(Files[i]);
    58.         }
    59.  
    60.         //Saves objects into *\Objects\
    61.         for (int i = 1; i < InstancesCreated + 1; i++)
    62.         {
    63.             GameObject.Find("Object_" + i).GetComponent<ObjectScript>().Save();
    64.             File.WriteAllText(Application.dataPath + "\\Games\\" + PlayerPrefs.GetString("Game") + "\\Objects\\" + i + ".json", JsonUtility.ToJson(GameObject.Find("Object_" + i).GetComponent<ObjectScript>()));
    65.         }
    66.  
    67.         //Save Game Variables (Instances Created)
    68.         File.WriteAllText(Application.dataPath + "\\Games\\" + PlayerPrefs.GetString("Game") + "\\gamevars.json", JsonUtility.ToJson(gameObject.GetComponent<SaveAndLoad>()));
    69.     }
    70.  
    71.     public void LoadGame()
    72.     {
    73.         Loading = true;
    74.         //Loads GameData
    75.         JsonUtility.FromJson<GameData>(File.ReadAllText(Application.dataPath + "\\Games\\" + PlayerPrefs.GetString("Game") + "\\gamevars.json"));
    76.         InstancesCreated = gameObject.GetComponent<GameData>().InstancesCreated;
    77.         GameObject.Find("ObjectSelectWindow").GetComponent<CreateObjectButton>().InsCreated = InstancesCreated;
    78.  
    79.         //Loads Objects
    80.         GameObject CubePref = GameObject.Find("ObjectSelectWindow").GetComponent<CreateObjectButton>().CubePref;
    81.  
    82.         Transform SpawnPosEmpty = GameObject.Find("ObjectSelectWindow").GetComponent<CreateObjectButton>().SpawnPosEmpty;
    83.  
    84.         for (int i = 0; i < InstancesCreated + 1; i++)
    85.         {
    86.             JsonUtility.FromJson<ObjectData>(File.ReadAllText(Application.dataPath + "\\Games\\" + PlayerPrefs.GetString("Game") + "\\Objects\\" + i + ".json"));
    87.             string ObjType = gameObject.GetComponent<ObjectData>().Type;
    88.             LoadingObjID = i;
    89.  
    90.             if (ObjType == "Cube")
    91.             {
    92.                 Instantiate(CubePref, new Vector3(SpawnPosEmpty.transform.position.x, SpawnPosEmpty.transform.position.y, SpawnPosEmpty.transform.position.z), new Quaternion(0, 0, 0, 1));
    93.             }
    94.         }
    95.  
    96.         Loading = false;
    97.     }
    98.  
    99. }
    100.  
    101. [System.Serializable]
    102. public class GameData
    103. {
    104.  
    105.     public int InstancesCreated;
    106.     public bool Loading;
    107.     public bool Startup;
    108.     public int LoadingObjID;
    109.  
    110. }
    111.  
    112. [System.Serializable]
    113. public class ObjectData
    114. {
    115.     public float[] Position;
    116.     public string Type;
    117. }
    118.  
    The error that I got was " 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) "

    Thanks!