- Home /
Question by
SlimBun · Apr 06, 2020 at 03:13 PM ·
error3derror messagepc
NullReferenceException: Object reference not set to an instance of an object, even what I am trying to get is not MonoBehaviour
Hello! I am making a game, And I got an error. This is my code:
Code I am getting the error from:
DifficultyData Data = SaveData.LoadDifficulty();
DifficultyLevel = Data.DifficultyLevel;
DifficultyData:
[System.Serializable]
public class DifficultyData
{
public float DifficultyLevel;
public DifficultyData(Difficulty DifficultyMuffin)
{
DifficultyLevel = DifficultyMuffin.DifficultyLevel;
}
}
SaveData:
public static void SaveDifficulty(Difficulty Difficulty)
{
BinaryFormatter Formatter = new BinaryFormatter();
string Path = Application.persistentDataPath + "/Difficulty.save";
FileStream Stream = new FileStream(Path, FileMode.Create);
DifficultyData Data = new DifficultyData(Difficulty);
Formatter.Serialize(Stream, Data);
Stream.Close();
}
public static DifficultyData LoadDifficulty()
{
string Path = Application.persistentDataPath + "/Difficulty.save";
if (File.Exists(Path))
{
BinaryFormatter Formatter = new BinaryFormatter();
FileStream Stream = new FileStream(Path, FileMode.Open);
DifficultyData Data = Formatter.Deserialize(Stream) as DifficultyData;
Stream.Close();
return Data;
}
else
{
BinaryFormatter Formatter = new BinaryFormatter();
FileStream Stream = new FileStream(Path, FileMode.Create);
int Data = 2;
Formatter.Serialize(Stream, Data);
Stream.Close();
BinaryFormatter FormatterReturn = new BinaryFormatter();
FileStream StreamReturn = new FileStream(Path, FileMode.Open);
DifficultyData DataReturn = FormatterReturn.Deserialize(StreamReturn) as DifficultyData;
Stream.Close();
return DataReturn;
}
}
(This is to save btw)
Comment
Answer by cwalshwarder · Apr 06, 2020 at 07:21 PM
Maybe it needs time to write to the file in the else statement.
Is it because it saves data as an int (int Data = 2) ?
Your answer
Follow this Question
Related Questions
Error when other people try to start my project. 1 Answer
Can't download assets in unity asset store 0 Answers
Keep getting this error message,Hi whenever i open the editor i keep getting this error. 0 Answers
Why have error: NullReferenceException: Object reference not set to an instance of an object 1 Answer
an unhandled win32 exception occurred in unity.exe [26640] 2 Answers