- Home /
Question by
ultraxdzn · Jul 25, 2018 at 04:53 PM ·
programmingjsonsave dataloading file
JSON not saving game object list
I was creating my inventory system when I came to save. After I wrote a script for it to save, I've been getting errors and also, when I would look up into a file I could see every value except list: https://gyazo.com/44b5c6d7d080eb2e785ce40f4e6e4094 Here are my codes: using System.Collections.Generic; using UnityEngine; using Newtonsoft.Json; using System.IO;
public class Savelist : MonoBehaviour
{
public static void Save(SavingAndLoading.RenameMe renameMe, string path)
{
File.WriteAllText(path, JsonConvert.SerializeObject(renameMe, Formatting.Indented));
}
public static SavingAndLoading.RenameMe Load(string path)
{
return JsonConvert.DeserializeObject<SavingAndLoading.RenameMe>(File.ReadAllText(path));
}
}
namespace SavingAndLoading
{
public class RenameMe
{
public float _positionX { get; set; }
public float _positionY { get; set; }
public float _positionZ { get; set; }
public float _rotationX { get; set; }
public float _rotationY { get; set; }
public float _rotationZ { get; set; }
public int XP { get; set; }
public int level { get; set; }
public int MaxHealth { get; set; }
public List<GameObject> ItemOrbsInInventory { get; set; }
}
}
Here I'm calling function
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using SavingAndLoading;
public class SaveAndLoad : MonoBehaviour {
public void Save()
{
RenameMe saveData = new RenameMe();
saveData.ItemOrbsInInventory = gameObject.GetComponent<Inventory>().ItemOrbsInInventory;
Savelist.Save(saveData, System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "player.dat");
print(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "player.dat");
}
void Load(){
SavingAndLoading.RenameMe loadData = Savelist.Load(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "player.dat");
gameObject.GetComponent<Inventory>().ItemOrbsInInventory = loadData.ItemOrbsInInventory;
}
void Start()
{
Load();
}
}
I think the issue is somewhere with saving. If someone knows how to fix this please answer me.
Comment
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Saving and loading different object types generated at runtime using JSON 0 Answers
Fail to load data from json to list of list. 1 Answer
Saves on android, could not find file 0 Answers
Save/Load System not working 0 Answers