Hello,
I followed the tutorial from unity about saving. It seems the video has been done in an older version (at least it seems so to me, as he is doing all gui stuff via script and not using the onClick from the editor).
Tutorial I used: Persistence: Saving and Loading Data - Unity Learn
So I re-did what he did except making the gui-stuff via scripts but by the editor.
Now I wanted to call the Save() and Load() via the onClick feature of the editor but they’re not in the list. Other functions appear as usual.
Also I am not getting how they got their variable values into the savescript. I don’t see any reference to the other script where the variables are stored.
Please help.
saveFunction.cs
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System;
using UnityEngine;
public class saveFunction : MonoBehaviour {
public void Save(){
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create(Application.persistentDataPath + "/save.ghoul");
PlayerData data = new PlayerData();
data.CurrentSouls = CurrentSouls;
bf.Serialize(file,data);
file.Close;
}
public void Load(){
if (File.Exists (Application.persistentDataPath + "/save.ghoul"))
{
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Open(Application.persistentDataPath + "/save.ghoul");
PlayerData data = (PlayerData)bf.Deserialize(file);
file.Close;
}
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
[Serializable]
class PlayerData
{
public float currentSouls;
}
current errors:
you have a typo. it’s lowercase for ‘c’ in “currentSouls” in the class, but you used capital …
one of the other errors is missing an argument for open. the filemode i believe.
you have 2 errors that are about “File.Close” because it should be “File.Close()” i’m pretty sure.
Report back after those fixes 
2 Likes
Thanks again @methos5k !
Fixed code:
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System;
using UnityEngine;
public class saveFunction : MonoBehaviour {
public void Save(){
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create(Application.persistentDataPath + "/save.ghoul");
PlayerData data = new PlayerData();
data.CurrentSouls = CurrentSouls;
bf.Serialize(file,data);
file.Close();
}
public void Load(){
if (File.Exists (Application.persistentDataPath + "/save.ghoul"))
{
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Open(Application.persistentDataPath + "/save.ghoul", FileMode.Open);
PlayerData data = (PlayerData)bf.Deserialize(file);
file.Close();
}
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
[Serializable]
class PlayerData
{
public float CurrentSouls;
}
Now the only open problem is my variable “CurrentSouls” which is stored in the script soulsController.cs is showing invalid, which I can fully understand as there is no reference to the other script. But in the video he doesn’t do that too and somehow it works (This is also what I hate about Video Tutorials, you can’t scroll through the script, you have to find those lines you seek in a timeline >.<)
You’re welcome. You work out the missing piece from there?
I’m trying to fiddle around with that GetComponent thingy. Hope that’s right.
Alright. I’ll check back later today, see how it goes 
Hello,
this is what I come up with, but no matter what I do, there’s always a problem with the CurrentSouls veriable
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System;
using UnityEngine;
public class saveFunction : MonoBehaviour {
private float CurrentSouls;
public void Save(){
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create(Application.persistentDataPath + "/save.ghoul");
PlayerData data = new PlayerData();
data.CurrentSouls = CurrentSouls;
bf.Serialize(file,data);
file.Close();
}
public void Load(){
if (File.Exists (Application.persistentDataPath + "/save.ghoul"))
{
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Open(Application.persistentDataPath + "/save.ghoul", FileMode.Open);
PlayerData data = (PlayerData)bf.Deserialize(file);
file.Close();
}
}
// Use this for initialization
void Start () {
GameObject SLVariables = GameObject.Find ("slVariables");
soulsController soulscontroller = SLVariables.GetComponent<soulsController> ();
CurrentSouls = soulscontroller.CurrentSouls;
}
// Update is called once per frame
void Update () {
}
}
[Serializable]
class PlayerData
{
public float CurrentSouls;
}
most recent error to this script is:
edit: What I didn’t think about yet, I’ll have to catch the variables loaded back into the soulsController.cs
If that’s your error, then somewhere on that line something isn’t reference properly. That’s not really related to saving/loading. Is SLVariables not found or something?
Could be, as it was just a try to copy something. In the example they used “ThePlayer” instead of SLVariables and “ThePlayer” was non existant anywhere too, thought that’s somewhat of a placeholder.
What I mean is that you can test if it’s found.
GameObject SLVariables = GameObject.Find ("slVariables");
if(SLVariables == null) print("Yep, not found.");
When I try to use your code snippet. Not working.
edit: https://drive.google.com/open?id=0B93lw8n5jxRfWjFObWFkMVB0S0E
The project atm, if that can help.
@methos5k 's snipplet was not supposed to solve your problem but identify it. I have no idea where you placed it, but if it’s anywhere other than before anything else in your Start function, it would probably have failed to be called anyway.
I agree with methos’s suspicions about the object you are trying to find, or how you are going about finding it, being the problem. Are you sure you have typed the name of the gameObject you are trying to find correctly?
If only one gameobject in your scene has the “soulsController” component, you can guarantee your search of the component with this instead:
soulsController soulscontroller = GameObject.FindObjectOfType<soulsController>();
1 Like
Hello,
my wording to the not working code snipped was bad… I had compiling errors using it. I know that this wasn’t the fully solution 
Your snipped worked very finde and I seem to have a working save file now.
But now I got the problem of loading it (at first manual via button and when it works, automatically at game start I guess it would be on Awake?)
When loading happens the variables from my save file are being loaded but how do I write them into the other script ? Is it similar to load variables from a different script?
Not sure I got your question exactly, but if the file is being loaded in Awake, then you can safely assign to the other class in Start(). This is good practice, to access non-local scripts in Start(), after every object has run Awake. 
Well… the automated on starting the game load will have to wait until I know how I write the loaded variable (data.CurrentSouls) into my CurrentSouls in the soulsController.cs
I am copying some stuff from google but until now I am not getting anywhere with that.
Oh, sorry you just want to know how to move it back? Okay, ya you just copy it back the opposite of how you got it for save 
I am trying but I can’t figure it out. Trying to use goole with “GameObject.FindObjectOfType write variable in other script” get’s me only stuff how to use that on getting but not setting variables 
I also changed the Name of the Variable while using save to not to confuse it with data on loading or saving as they both reference to the private CurrentSouls. Did I do that right ? Also I don’t understand the last lines with the serialization. So I didn’t touch it yet.
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System;
using UnityEngine;
public class saveFunction : MonoBehaviour {
private float CurrentSoulsSave;
private float CurrentSoulsLoad;
public void Save(){
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create(Application.persistentDataPath + "/save.ghoul");
PlayerData data = new PlayerData();
data.CurrentSouls = CurrentSoulsSave;
bf.Serialize(file,data);
file.Close();
}
public void Load(){
if (File.Exists (Application.persistentDataPath + "/save.ghoul"))
{
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Open(Application.persistentDataPath + "/save.ghoul", FileMode.Open);
PlayerData data = (PlayerData)bf.Deserialize(file);
CurrentSoulsLoad = data.CurrentSouls;
//GameObject.FindObjectOfType<soulsController>().CurrentSouls = CurrentSoulsLoad;
file.Close();
}
}
// Use this for initialization
void Start () {
soulsController soulscontroller = GameObject.FindObjectOfType<soulsController>();
CurrentSoulsSave = soulscontroller.CurrentSouls;
}
// Update is called once per frame
void Update () {
}
}
[Serializable]
class PlayerData
{
public float CurrentSouls;
}
Here… can you just make a variable:
public soulsController SoulsController; // drag & drop in the inspector
Then, when you call load/save:
// Save
data.CurrentSouls = SoulsController.CurrentSouls;
// Load almost the same
SoulsController.Currentsouls = data.CurrentSouls;
That would be the easiest. You can get rid of those 2 save/load variables in that class/script you have posted here.
This is more direct and also avoids your find object.
1 Like
Damn it’s that easy?
Works like a charm. Save/Load automization was pretty easy to achieve too. Big thanks !
edit: Sorry for the late replies, but I only have 2x90 minutes each day to work on this.
It’s all good. Glad ya got it working.