Can someone tell me what is wrong with this script?
I got this this script for the RPG tutorial series from HardlyBrief Programming in the Serialization episode. Here's the script:
using UnityEngine;
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
public class PPSerialization {
public static BinaryFormatter binaryFormatter = new BinaryFormatter();
public static void Save(string saveTag, object obj)
{
MemoryStream memoryStream = new MemoryStream();
binaryFormatter.Serialize(memoryStream, obj);
string temp = System.Convert.ToBase64String(memoryStream.ToArray());
PlayerPrefs.SetString(saveTag, temp);
}
public static void Load(string saveTag)
{
string temp = PlayerPrefs.GetString(saveTag);
if(temp == string.Empty)
{
return null;
}
MemoryStream memoryStream = new MemoryStream(System.Convert.FromBase64String(temp));
return binaryFormatter.Deserialize(memoryStream);
}
Visual Studio marks the "return null" and "return binaryFormatter.Deserialize(memoryStream)".
This is the problem: CS0127 "Since 'PPSerialization.Load(string)' returns void, a return keyword must not be followed by an object expression".
return binaryFormatter.Deserialize(memoryStream);
what is the data type you are trying to return?
you've got the error because its a void method a void method does not return a value public static void Load(string saveTag)
So what should I do? Why in the video he doesn't got any problem?
Answer by MarkTerence · Jun 13, 2016 at 01:58 PM
change public static void Load(string saveTag)
to public static object Load(string saveTag)
Thanks it works perfectly. But I have one question:
PlayerPrefs.GetInt("HEALTH", health_reference); = PPSerialization.Save("HEALTH", health_reference); ?
replacing the PlayerPrefs.GetInt("HEALTH", health_reference);
to PPSerialization.Save("HEALTH", health_reference);
to save the health ?
wait, since you called PPSerialization.Save is it really PlayerPrefs.GetInt("HEALTH", health_reference); or PlayerPrefs.SetInt("HEALTH", health_reference);?
I'm saying if I use the PPSerialization.Save("PLAYERHEALTH", playerhealth_reference)
will it work like PlayerPrefs.GetInt("PLAYERHEALTH", playerhealth_reference)
?
Your answer
Follow this Question
Related Questions
How do you copy an image from the clipboard into an ui image? 0 Answers
Errors in script when trying to play animation clips 1 Answer
Cannot Convert from NewGame to CubeAI 0 Answers
How to use the GPGS ISavedGameMetadata? 1 Answer
Can someone please help me find out what wrong with my code. 0 Answers