- Home /
loading file from instantiate button problem?
trying to create save and load system with instantiate button the problem this the file saved the info but when i try to load the savedData nothing change the Data wont load my knowledge lvl wouldn't help me in this to figure out
public void savedfiles(Cube player)
{
string folderPath = Path.Combine(Application.persistentDataPath, foldername);
filesavename = savename.text;
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(folderPath + "/" + filesavename + fileextnison);
PData playerdata = new PData(player);
bf.Serialize(file, playerdata);
file.Close();
}
public void loaded()
{
string folderPath = Path.Combine(Application.persistentDataPath, foldername);
string[] filePaths = Directory.GetFiles(folderPath, "*.txt");
foreach (string g in filePaths)
{
var onlyFileName = Path.GetFileNameWithoutExtension(g);
GameObject button = (GameObject)Instantiate(buttonprefab);
button.GetComponentInChildren<Text>().text = onlyFileName;
button.transform.SetParent(menupanle,false);
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(g, FileMode.Open);
PData playerdata = (PData)bf.Deserialize(file);
Debug.Log(onlyFileName);
}
void Start() {
string folderPath = Path.Combine(Application.persistentDataPath, foldername);
if (!Directory.Exists(folderPath))
Directory.CreateDirectory(folderPath);
} }
Have you checked if the 'g' variable holds the complete path to the file or just the filename?
I just also noticed that you are assigning the deserialized data to a local variable that will be lost when the function returns.
i check that from debug.log and its hold the complete path.
Answer by lTimesl · Mar 06, 2019 at 11:31 AM
i fix the problem it was in the end of the load function i forget to close the file using file.clos() this is my updated code its work perfectly just need the save and load checks like empty filenames etc hope this help any one search for simple multi save and load system; public class SaveSystem : MonoBehaviour { public InputField savename; public GameObject buttonprefab; public Transform menupanle; const string foldername = "Save12"; const string fileextnison = ".txt"; string filesavename;
void Start()
{
string folderPath = Path.Combine(Application.persistentDataPath, foldername);
if (!Directory.Exists(folderPath))
Directory.CreateDirectory(folderPath);
}
public void SaveData1(Move Player)
{
string folderPath = Path.Combine(Application.persistentDataPath, foldername);
filesavename = savename.text;
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(folderPath+"/"+ filesavename + fileextnison);
PlayerDate Data = new PlayerDate(Player);
bf.Serialize(file, Data);
file.Close();
}
public void InstantiateButton(Move Player)
{
string folderPath = Path.Combine(Application.persistentDataPath, foldername);
string[] filePaths = Directory.GetFiles(folderPath, "*.txt");
foreach (string g in filePaths)
{
var onlyFileName = Path.GetFileNameWithoutExtension(g);
GameObject button = (GameObject)Instantiate(buttonprefab);
button.GetComponentInChildren<Text>().text = onlyFileName;
button.GetComponent<Button>().onClick.AddListener(
() => { loading(g, Player); });
button.transform.SetParent(menupanle, false);
}
}
public void loading(string S,Move Player)
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(S, FileMode.Open);
PlayerDate Data = (PlayerDate)bf.Deserialize(file);
file.Close();
Player.score = Data.score;
Player.level = Data.level;
Vector3 postaion;
postaion.x = Data.posation[0];
postaion.y = Data.posation[1];
postaion.z = Data.posation[2];
Player.transform.position = postaion;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612192959im_/https://answers.unity.com/themes/thub/images/avi.jpg)