- Home /
Loading player data using data persistence
Hi, I am learning to how to save player data using data persistence and I have encountered an error which has me confused. In the tutorial I am watch it is in C# however I am using UnityScript. The following screenshot is the tutorial and the code snippet is what I have wrote.
if(File.Exists(Application.persistentDataPath + "/gameInfo.dat")){
var binForm : BinaryFormatter = new BinaryFormatter();
var file : FileStream = File.Open(Application.persistentDataPath + "/gameInfo.dat", FileMode.Open);
var data : playerData = (playerData)binForm.Deserialize(file);
file.Close();
I thought I had translated it correctly however line 4 of my code snippet where I am deserialising the file, I get an error which is expecting a semicolon at the end of that line. Any idea why this might be happening? thanks
I'm not 100% sure but shouldn't the (playerData) be (PlayerData) Caps`var data : playerData = (PlayerData)binForm.Deserialize(file);`
I am referencing the class I made below which is called "playerData", I did have it called "PlayerData" in which case I wrote what you suggested. Below is the class I am using.
class playerData{
var bloxAmount : int;
var currentSkin : Sprite;
var currentColor : Color;
}
Answer by AurimasBlazulionis · Jan 28, 2017 at 08:52 PM
The syntax problem is with typecasting. Instead of (playerData)binForm.Deserialize(file)
, type binForm.Deserialize(file) as playerData
.
Your answer
![](https://koobas.hobune.stream/wayback/20220612105821im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Do u need to learn Swift in order to transfer ur data (strings, int, etc.) to Xcode? 0 Answers
build file with one game and two game can access it 0 Answers
"IOException: Sharing violation on path" when Loading Data to Overwrite Save 1 Answer
Data Management Issue - How to manage retrieved data from mysql? 0 Answers
[Quiz game] How do I make players contribute with questions? 1 Answer