- Home /
Problem when saving and loading
I am geting these varnings in the unity 5 console: The class named 'LoadandSave' is not derived from MonoBehaviour or ScriptableObject! GameObject (named 'GameSpawner') references runtime script in scene file. Fixing! But I am not getting any errors or varnings in Visual Studio.
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public class GameData : MonoBehaviour
public Text ScoreText;
public Text Highscoretext;
public Transform player;
public static float Highscore = 0;
void Update()
{
ScoreText.text = player.transform.position.x.ToString("0");
if (player.transform.position.x > Highscore)
{
Highscoretext.text = player.transform.position.x.ToString("0");
}
}
}
public class LoadandSave
{
private void Awake()
{
Save();
}
public static void Save()
{
BinaryFormatter binaryformatter = new BinaryFormatter();
using (FileStream fs = new FileStream("gamesave.bin", FileMode.Create, FileAccess.Write))
{
binaryformatter.Serialize(fs, GameData.Highscore);
}
SceneManager.LoadScene(0);
}
public static void Load()
{
if (!File.Exists("gamesave.bin"))
{
return;
}
BinaryFormatter binaryformatter = new BinaryFormatter();
using (FileStream fs = new FileStream("gamesave.bin", FileMode.Open, FileAccess.Read))
{
GameData.Highscore = (int)binaryformatter.Deserialize(fs);
}
}
}
I have tried adding :MonoBehaviour att the end of public class LoadandSave. But then the unity 5 editor wont start play. And just craches.
I guess you forgot to put a { after $$anonymous$$onoBehaviour
public class GameData : $$anonymous$$onoBehaviour {
Answer by Merrick20 · Sep 19, 2017 at 09:01 PM
If I'm reading this correct you miss a { after public class GameData : MonoBehaviour
this is why it doesn't compile the rest of the script. Are you doing this on an object? Why are you not using two different files for the scripts?
Your answer
![](https://koobas.hobune.stream/wayback/20220612135203im_/https://answers.unity.com/themes/thub/images/avi.jpg)