- Home /
Save GameObject to file without Playerpref
Hi! So I am trying to save everything in my level and with the state they were. EX: If a door was broken, it stays broken. Door was unlocked, it stays unlocked. etc.
.
But All I can find currently find online are lacking a lot of informations and it is really frustrating.
.
I have done multiple test with just the player and I can't understand why it doesn't work and what is missing to make it work..
. I would like to be able to save everything on my player : All its components, all its children and all the childrens' children (including all their component). In a way, I would sort of think of having the similarity of a prefab of the player for lets say the "save A" or do what unity does when loading a level and remembering exactly where everything was and what were their components etc.
.
Here is a sample of what I tried so far :
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
public static class Load
{
//Path of the saves
static string SavesPath;
public static GameObject LoadPlayer()
{
SavesPath = GameManager.Instance.GetSavesPath;
if(File.Exists(SavesPath + "/save.json"))
{
try
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(SavesPath + "/save.json", FileMode.Open);
//Get the data
string data = (string)bf.Deserialize(file);
//Close the file
file.Close();
//Convert the Data into the an Object
SerializedGameObject player = JsonUtility.FromJson<SerializedGameObject>(data);
Debug.Log(player.gameObject);
//Return the gameObject
return player.gameObject;
}catch(System.Exception e)
{
Debug.Log(e);
}
}
return null;
}
}
public static class Save
{
//Path of the save
static string SavesPath;
/// <summary>
/// Save instance of the player
/// </summary>
public static void Player()
{
//Get the save path from the GameManager
SavesPath = GameManager.Instance.GetSavesPath;
//Look if the save folder exist
CheckSaveFolder();
//Look if the save file exists
CheckSaveFile();
try
{
//Get the gameobject of the player
SerializedGameObject player = new SerializedGameObject();
player.gameObject = PlayerManager.Instance.gameObject;
//Stringify the GameObject
string data = JsonUtility.ToJson(player);
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(SavesPath + "/save.json", FileMode.Open);
bf.Serialize(file,data);
file.Close();
Debug.Log("Player saved successfully!");
}catch(System.Exception e)
{
Debug.Log(e);
}
}
}
using UnityEngine;
/// <summary>
/// Allow to serialization of a GameObject
/// </summary>
[System.Serializable]
public class SerializedGameObject
{
public GameObject gameObject;
}
Answer by matthewroxby · Feb 05, 2021 at 09:55 PM
What you could do instead is have a dictionary of type string,int (or whatever you may need, maybe 0 corresponds to a broken door, 1 to an unlocked one) and have the string correspond to the gameobject name. Then when you reload your scene, just loop through the list to find all your objects and set their individual states. I'm not very well versed on .json files, but you could actually do this with playerPrefs, just do SetInt(objectName , int state)
Answer by toske_ · Feb 12, 2021 at 08:09 PM
you use a scriptable object aswell, they story data and retain it.
Your answer
Follow this Question
Related Questions
Save GameObject to file without Playerpref 0 Answers
Why doesent my code get executed? 1 Answer
How can i add all the prefabs in the assets directory and sub directories to List or Array ? 0 Answers
How do I get an object to Instantiate infront of a specific object/player? 1 Answer
How can i add a second camera that will be showing only specific gameobject in game window ? 1 Answer