Hello,
I’m new in topics like saving game progress and I have a question. What I need is to save whole GameObject to file on button click (when game is closing) and later when turning game again on I want to Instantiate that GameObject from saved file. Is there any way to do that? What i tried now is:
Doing class with GameObject:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class SaveLevel
{
public GameObject Object;
public SaveLevel(GameObject object)
{
Object = object;
}
}
And voids for buttons to save and load it:
public void SaveLevelOnButton()
{
SaveLevel save = new SaveLevel(/*GameObject i want to save. Let's say:*/ ObjectToSave);
string json = JsonUtility.ToJson(save);
File.WriteAllText(Application.persistentDataPath + "/LevelState.json", json);
}
public void LoadLevelOnButton()
{
GameObject LoadedLevel;
string json = File.ReadAllText(Application.persistentDataPath + "/LevelState.json");
LoadedLevel = JsonUtility.FromJson<SaveLevel>(json).gameObject;
Instantiate(LoadedLevel, new Vector3(0, 0, 0), Quaternion.identity);
}
That code is creating the GameObject “ObjectToSave” from begin like it wasn’t saved.