I have error in my game saving script (C#).
this is error: Assets/Scripts/LoadGame.cs(48,2): error CS0246: The type or namespace name `serializable' could not be found. Are you missing a using directive or an assembly reference?
I want to know what's wrong.
using UnityEngine;
using System.Collections;
using System;
using System.Runtime.Serialization.Formatters.Binary;
public class LoadGame : MonoBehaviour
{
public Turn turn;
public void Save()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persidentDataPath + "/playerInfo.dat");
PlayerData data = new PlayerData();
turn.Cash = Cash;
bf.Serialize(file, data);
file.Close();
}
public void Load()
{
if (File.Exists(Application.persidentDataPath + "/playerInfo.dat"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persidentDataPath + "/playerInfo.dat", FileMode.Open);
file.Close();
Cash = turn.Cash;
}
}
}
[serializable]
class PlayerData
{
public int Cash;
}
isn't it Serializable
?
also, it looks like the closing brace on line 9 shouldn't be there...
Answer by Dave-Carlile · Jun 22, 2016 at 08:33 PM
Most computer languages are case sensitive, meaning serializable
and Serializable
aren't the same thing. The attribute in this case is Serializable
.
Now I have this error Assets/Scripts/LoadGame.cs(27,17): error CS0246: The type or namespace name `FileStream' could not be found. Are you missing a using directive or an assembly reference?
FileStream should be a part of System.IO, did you try importing it ?
After that i have this errors: Assets/Scripts/LoadGame.cs(28,59): error CS0117: UnityEngine.Application' does not contain a definition for
persidentDataPath' Assets/Scripts/LoadGame.cs(28,40): error CS1502: The best overloaded method match for System.IO.File.Create(string)' has some invalid arguments Assets/Scripts/LoadGame.cs(28,40): error CS1503: Argument
#1' cannot convert object' expression to type
string' Assets/Scripts/LoadGame.cs(31,29): error CS0103: The name Cash' does not exist in the current context Assets/Scripts/LoadGame.cs(38,45): error CS0117:
UnityEngine.Application' does not contain a definition for persidentDataPath' Assets/Scripts/LoadGame.cs(38,26): error CS1502: The best overloaded method match for
System.IO.File.Exists(string)' has some invalid arguments Assets/Scripts/LoadGame.cs(38,26): error CS1503: Argument #1' cannot convert
object' expression to type string' Assets/Scripts/LoadGame.cs(41,65): error CS0117:
UnityEngine.Application' does not contain a definition for persidentDataPath' Assets/Scripts/LoadGame.cs(41,48): error CS1502: The best overloaded method match for
System.IO.File.Open(string, System.IO.File$$anonymous$$ode)' has some invalid arguments Assets/Scripts/LoadGame.cs(41,48): error CS1503: Argument #1' cannot convert
object' expression to type string' Assets/Scripts/LoadGame.cs(44,25): error CS0103: The name
Cash' does not exist in the current context
What is your build target? File IO functionality isn't available for the web.
Once that is resolved, you really need to find some C# tutorials to learn the basics. Learning to resolve compiler errors through forum questions isn't the best use of anyone's time.
Assets/Scripts/LoadGame.cs(12,59): error CS0117: UnityEngine.Application' does not contain a definition for
persidentDataPath' Assets/Scripts/LoadGame.cs(12,40): error CS1502: The best overloaded method match for System.IO.File.Create(string)' has some invalid arguments Assets/Scripts/LoadGame.cs(12,40): error CS1503: Argument
#1' cannot convert object' expression to type
string' Assets/Scripts/LoadGame.cs(15,29): error CS0103: The name Cash' does not exist in the current context Assets/Scripts/LoadGame.cs(15,17): error CS0103: The name
tura' does not exist in the current context Assets/Scripts/LoadGame.cs(22,45): error CS0117: UnityEngine.Application' does not contain a definition for
persidentDataPath' Assets/Scripts/LoadGame.cs(22,26): error CS1502: The best overloaded method match for System.IO.File.Exists(string)' has some invalid arguments Assets/Scripts/LoadGame.cs(22,26): error CS1503: Argument
#1' cannot convert object' expression to type
string' Assets/Scripts/LoadGame.cs(25,65): error CS0117: UnityEngine.Application' does not contain a definition for
persidentDataPath' Assets/Scripts/LoadGame.cs(25,48): error CS1502: The best overloaded method match for System.IO.File.Open(string, System.IO.File$$anonymous$$ode)' has some invalid arguments Assets/Scripts/LoadGame.cs(25,48): error CS1503: Argument
#1' cannot convert object' expression to type
string' Assets/Scripts/LoadGame.cs(28,32): error CS0103: The name tura' does not exist in the current context Assets/Scripts/LoadGame.cs(28,25): error CS0103: The name
Cash' does not exist in the current context
Your answer
Follow this Question
Related Questions
How to Destroy game object horizontally and vertically , when hit by a Raycast 0 Answers
Always wondered how to sync animations with attack effects 0 Answers
Adding a Highscore to game 1 Answer
SPACE SHOOTER SHOOTING SHOTS TUTORIAL HELP (UNITY 5 C#) 3 Answers
C# Script not working as expected 2 Answers