- Home /
Question by
bewilderstudiosgames · Aug 01, 2018 at 12:31 AM ·
serializationloadsave datafilestream
serialization stream supports seeking but its length is 0
Hi! Having an issue with Serializables... I've been watching THIS VIDEO on how to save and load files, and yet it still doesn't manage to work for me.
This game is going on android and it might be different, but I want it to save when the app is tabbed out and load on the game start, I currently have it saving every second because I wanted to make sure it works.
Most of this code has nothing to do with this but some does.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
using UnityEngine.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public class Click : MonoBehaviour {
public float tobys;
public int tobysPerClick;
public ItemManager[] items;
public float TobyPS;
public TMPro.TMP_Text TobyDisplay;
public TMPro.TMP_Text TobyPSDisplay;
public ItemManager itemManager;
public GameObject TobyFall;
GameObject TobyClone;
public Image Tobyimage;
public Sprite[] sprites;
public Settings settings;
public static float TotalTobysCollected;
public static float TotalTobysPerSecond;
public static int TotalTobysPerClick;
// Use this for initialization
void Start () {
StartCoroutine(AutoTick());
if (File.Exists(Application.persistentDataPath + "/clickData.dat"))
{
// THIS LOADS THE FILE
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/clickData.dat", FileMode.Open);
ClickData data = (ClickData)bf.Deserialize(file);
TotalTobysCollected = data.Total_Tobys;
TotalTobysPerClick = data.Total_Tobys_Per_Click;
TotalTobysPerSecond = data.Total_Tobys_Per_Second;
file.Close();
}
InvokeRepeating("UpdateTime", 0, 1.0f);
}
// Update is called once per frame
void Update () {
TobyPS = GetTobyPerSecond();
TotalTobysCollected = tobys;
TotalTobysPerClick = tobysPerClick;
TotalTobysPerSecond = TobyPS;
}
void UpdateTime()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/clickData.dat");
ClickData data = new ClickData();
data.Total_Tobys = TotalTobysCollected;
data.Total_Tobys_Per_Click = TotalTobysPerClick;
data.Total_Tobys_Per_Second = TotalTobysPerSecond;
bf.Serialize(file, data);
file.Close();
}
}
[Serializable]
class ClickData{
public float Total_Tobys;
public float Total_Tobys_Per_Second;
public int Total_Tobys_Per_Click;
}
Comment
Just a note that I removed some of the code as it had no relevance to the issue