I am trying to use a script that load the player’s floats
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public static class SaveManager
{
public static void SavePlayer(NewPlayer Player)
{
BinaryFormatter bf = new BinaryFormatter();
FileStream stream = new FileStream(Application.persistentDataPath + "/player.sav", FileMode.Create);
PlayerData data = new PlayerData(Player);
bf.Serialize(stream, data);
stream.Close();
}
public static float[] LoadPlayer() <-------- Error
{
if(File.Exists(Application.persistentDataPath + "/player.sav"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream stream = new FileStream(Application.persistentDataPath + "/player.sav", FileMode.Open);
PlayerData data =bf.Deserialize(stream); <---------- Error
}
}
}
[Serializable]
public class PlayerData
{
public float[] Stats;
public PlayerData(NewPlayer Player)
{
Stats = new float[4];
Stats[0] = Player.Attack;
Stats[1] = Player.Level;
Stats[2] = Player.Money;
Stats[3] = Player.GunAttack;
}
}
I believe you have to cast to PlayerData when you deserialize.
In addition to that, not all code paths return the valid type of the method. (in fact, none do)
I imagine you’re looking to return data.Stats or null.
I have a red mark under;
I am so frustrated man, do you have any easy way to save parameters(Android)?
I can use PlayerPrefs but i heard its easy to be hacked right?
Sorry, but “red mark under” is not an error or message, just a visual hint
Sure, player prefs could be modified more easily, but binary would not be impossible.
It depends on the data and how worried you are.
Did you not understand my response about the return value of the method? That has nothing to do with saving, and is just a general thing in programming / C#.
I never learned programming, all i know is from the internet.
I use Unity i think less than a month.
Can you edit my code or it would be too much for you?I don’t know what to do,Thanks.
Well, you will have one issue after another if you do not try to learn some programming in C# for your use with Unity. You do not have to become the world’s greatest programmer, but you should at least learn the basics + go up from there , as you work on your game/Unity stuff.
In the future, if you are really stuck on something and/or don’t understand an error. Look at the error, and try to look it up online. If you do a forum post, post the error, if you’re still confused. Like I said before, “red line” is not a real message.
I can try, I think maybe like this will work:
public static float[] LoadPlayer() <-------- Error
{
if(File.Exists(Application.persistentDataPath + "/player.sav"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream stream = new FileStream(Application.persistentDataPath + "/player.sav", FileMode.Open);
PlayerData data = (PlayerData)bf.Deserialize(stream); <---------- Error
return data.Stats;
}
return null;
}
You must check the return value to ensure it’s not null before you use it.
Let me know if that works, or if not, what error it has now
One other thing to consider, that you mentioned earlier yourself. PlayerPrefs. Even for a non-programmer, the code for that is even simpler. If you have other stuff to work on, using PlayerPrefs can allow you to see how your saved/loaded data is working, while continuing to develop your game and you can always return to update the save format.
This way you won’t be stuck on this issue forever…
Man, Thank you very much for your help, i really appreciate it!!
I learned Java at school but its abit different and it was few years ago, with my current knowledge i think i can programm almost anything that i want.
I dont know how to start to learn unity combined with C#, i just follow tutorials, check my problems at the internet and as you see i ask here, i learn just from my experience. So far i had a big progress i think.
And it’s harder for me because English is not my first language.
Anyway, thanks
Ya, that could be an issue if maybe the file is still in use or something?
To be honest, I’d have to look it up for a refresher. I am pretty certain that you can specify a share mode for reading, though. Again, I don’t have it memorized, but if you google that error/violation, you will probably find related topics, issues & resolutions.
You’re welcome. Glad you’re having fun. For sure, learning as you go is good, too.
There are programming tutorials/posts in the learn section of this website (programming/scripting subsection), for Unity + C#.
Yea i added that, just had something else in my script that i needed to change
Another issue:
I have a menu with Loading and New Game, i am trying to open a scene with the new load by using GameObject that pass from scene to scene.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewGame : MonoBehaviour {
public bool OpenNewGame;
public static GameObject instance;
void Awake()
{
if (instance == null) { instance = this.gameObject; } else { Destroy(gameObject); return; }
DontDestroyOnLoad(gameObject);
}
void Start () {
if (!OpenNewGame)
{
FindObjectOfType<NewPlayer>().Load();
}
}
}
I’m not entirely certain what your question is, sorry?
Are you hoping that start will run on each scene? It will only run once, since the object persists, if that’s relevant.
Otherwise, could you please try to explain it, again?
So you have an object called Player that has a function called Load()
So you have 2 Scene
1 the Menu (without the player?)
2 the level (with the player?)
And you want the player to call a function called Load()?
“So what i was trying to do is to make a GameObject at the Opening Scene with a bool (OpenNewGame),If it is true so Load the file.” : What you can do is create a Static class that will contain this boolean
On game start in the Player beginplay or awake just call your Load() If the bool === true