Reputation: 49
I am going to make a save system to save my highscore in my unity game but gives me two errors this is first code to data,,,
public class SaveHighScore
{
public int highScore;
public SaveHighScore(HighScore highscore)
{
highScore = highscore.highScore;
}
}
this is my second one to Save System
public class SaveSystem
{
public static void SaveScore(HighScore highscore)
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Application.persistentDataPath + "/player.fun";
FileStream stream = new FileStream(path, FileMode.Create);
SaveHighScore data = new SaveHighScore(highscore);
formatter.Serialize(stream, data);
stream.Close();
}
public static SaveHighScore loadHighScore()
{
string path = Application.persistentDataPath + "/player.fun";
if (File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
HighScore data = formatter.Deserialize(stream) as SaveHighScore;
stream.Close();
return (SaveHighScore) data;
}
else
{
Debug.Log("no highScore found");
return null;
}
}
this is my third and last code
public class HighScore : MonoBehaviour
{
public int highScore;
public void saveHighscore()
{
SaveSystem.SaveScore(this);
}
public void loadHighscore()
{
SaveHighScore data = SaveSystem.loadHighScore();
highScore = data.highScore;
}
}
first code is to prepare data to save. second code is to make save system. third one is to make two functions to call when the player wants to load last high score.
but there gives two errors in second code.
SaveSystem.cs(24,30) Cannot implicitly convert type 'SaveHighScore' to 'HighScore'
and
SaveSystem.cs(26,20) Cannot implicitly convert type 'HighScore' to 'SaveHighScore'
i'm finding a solution for these errors. i don't know how to solve these.
Anyone here to help me out????
Upvotes: 1
Views: 476
Reputation: 5108
HighScore data = formatter.Deserialize(stream) as SaveHighScore;
You're deserializing a SaveHighScore
but trying to save it in a HighScore
, which is a MonoBehaviour component, and not at all the same thing you wish it to be.
Modify your code to
var data = formatter.Deserialize(stream) as SaveHighScore;
and I think everything should work.
Upvotes: 1