- Home /
Unable to save game
I'm try a few days to learn how to keep the data in the game I want to keep a number for learn, but I do not understand why I can not
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
[System.Serializable]
public class Save
{
public int Cuens;
public GameObject test;
void Start()
{
test.AddComponent<Game> ();
}
void Update ()
{
if (Input.GetKey (KeyCode.T)) {
Cuens += 5;
SaveGame ();
}
if (Input.GetKey (KeyCode.Y))
LoadGame ();
}
void SaveGame()
{
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create (Application.persistentDataPath + "/gamesave.save");
Game save = new Game (Cuens);
bf.Serialize (file, save);
file.Close ();
Debug.Log (save);
}
void LoadGame()
{
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Open (Application.persistentDataPath + "/gamesave.save", FileMode.Open);
Game save = (Game)bf.Deserialize (file);
file.Close ();
Debug.Log (save);
}
}
Comment
Your Save
class does not inherit from $$anonymous$$onoBehaviour
, so it can't be attached to a GameObject and can't run the Start
nor Update
methods.... Is it a typo?
Did you try to attach the debugger and see what's wrong? Did anything get written to the file? Is saving failing, or loading? Does SaveGame() even get called? Use the debugger
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Flip over an object (smooth transition) 3 Answers
Syncing variables between two objects of same script 1 Answer
Real-time and historical input telemetry 0 Answers