Hi, Unity Community! I’m trying to experiment using persistantDataPath for Java/Unity Script, but I’ve became stuck. How can I correct line 6 which is the C# version of this but I’m shooting for Java, and could someone also tell me if line 10 is correct in Java? I don’t know what the Java version of C#'s [Serializable]. Thank you!
public function Load(){
if(File.Exists(Application.persistentDataPath + "/SavedGame.ink")){
bf = new BinaryFormatter();
file = File.Open(Application.persistentDataPath + "/SavedGame.ink");
data = (SavedData)bf.Deserialize(file);
file.Close();
}
}
@SerializeField
class SavedData{
var savePosition : Vector3;
}
Your serialization code looks fine to me. Unfortunately you can’t serialize Vector3 structs. (which is a huuuuge pain I might add!).
To fix this you either write a wrapper class and serialize it yourself or you just do something like:
class savedData {
float savePosX;
float savePosY;
float savePosZ;
}
Oh, and little side note. You mean java script. Not java. They have actually very little in common. Even c# is closer to Java then java script is. Yah, I know, pretty weird eh?
JavaScript and Java are not the same thing. Not by a long shot. You’re looking for Javascript help.
JavaScript is named JavaScript because Java was really fresh and cool when JavaScript came out. They barely look and they function completely differently. There is no Java in Unity - the closest thing is actually C#, not JavaScript
Good idea! I had to do the same thing when I used PlayerPrefs but I didn’t know it wouldn’t work with serialized data. Also, thanks for clearing up the Java/Java Script thing(ALL OF YOU! LOL)
However, I still get an error saying that I’m missing a semicolon on line 6, so I really believe there’s a syntax error. Remember, line 6 is C#, and I need help converting it to Java Script.
Had to actually look that up myself but aparently unity javascript does type casting a little different (found this: LINK ).
What your line 6 is currently doing is calling a function called Deserialize with the parameter “file”. Deserialize returns data of type Object. Now usually you need to tell your programm to convert your data to whatever type you need it as. In your case that would be SavedData.
But apparently Unity JavaScript does that on its own. So if you change line 6 to:
Thank you, you’re awesome. It worked! Here’s the full script if anyone is interested. If anyone has any idea how I can save other things like object information, SetActive information, game state/progression, etc, please lend some advice. Thanx: js.
import System;
import System.IO;
import System.Runtime.Serialization.Formatters.Binary;
var player : Transform;
function Start(){
player = GameObject.FindGameObjectWithTag("Player").transform;
}
function Update(){
if(Input.GetKeyDown("s"))
Save();
if(Input.GetKeyDown("l"))
Load();
}
public function Save(){
bf = new BinaryFormatter();
file = File.Create(Application.persistentDataPath + "/SavedGame.ink");
data = new SavedData();
data.X = transform.position.x;
data.Y = transform.position.y;
data.Z = transform.position.z;
bf.Serialize(file,data);
file.Close();
}
public function Load(){
if(File.Exists(Application.persistentDataPath + "/SavedGame.ink")){
bf = new BinaryFormatter();
file = File.Open(Application.persistentDataPath + "/SavedGame.ink",FileMode.Open);
data = bf.Deserialize(file) as SavedData;
file.Close();
player.transform.position.x = data.X;
player.transform.position.y = data.Y;
player.transform.position.z = data.Z;
}
}
@SerializeField
class SavedData{
var X : float;
var Y : float;
var Z : float;
}