- Home /
Store Playerdata in Scriptable Object
Hello,
Actually i try to use Scriptable Object as Data Container. So far as good he raise up the Variable as example a Counter
I destroy a Brick and he tell the Scriptable Object i destroyed a brick and increase the int by 1 Working Fine
But as soon i stop Playing an restart the Game all Data in the Scriptable Object are gone. I was thinking Data in Scriptable Object are persisant and stay at the Values. Because i need Access from various Scripts i made a PlayerManager:
public Player player;
public static PlayerManager playerManager;
private void Awake()
{
playerManager = this;
DontDestroyOnLoad(this);
}
he is the Reference in every Scene to get Access to the Player Scriptable Object which only look like this:
using UnityEngine;
[CreateAssetMenu(fileName = "Player", menuName = "Player")]
public class Player : ScriptableObject
{
public int brickCounter;
public float score;
public float highscore;
}
My Idea behind that is simple, sure i can store that information in PlayerPrefs, but PlayerPrefs are susceptible to Cheater and i someone Know where to look he find them easy. Sure i can make as example a JSON and make a big Security behind that all, but for a small game like mine im not sure if its economic to make so much effort to prevent player from cheating.
So when i set Data in a Scriptable Object like i say in Inspector brickCounter = 100; i close the Game and restart the game the brickCounter is still at 100;
But if i start the game play a bit and destroy some Bricks the Counter go from 100 to 105 (i destroyed now 5 brick) then i can see in the Scriptable Object its 105. But as soon i stop the Game and start again it is set back to 100. Why is that? How can i solve this?
Hope someone can give me a hint or idea to solve it different way. I have EasySave2 Asset but as i said..... its a small game....
Answer by DerDerErIst · Jan 31, 2018 at 02:41 PM
As i go through my Code i consider about.
My PlayerManager is a static so i have access all over the scene to it. Means that my Player player (the Scriptable Object) is also a static? That would explain to me why he reset the Scriptable Object after i restart the Game?
If i make it non Static and make something like FindGameObject would this work?
Mhh okay i tried to make my PlayerManager non static and get in all Scripts a reference to that PlayerManager but sadly he still delete all information if i restart the Game/Unity
Answer by grizzly48 · Jan 31, 2018 at 03:18 PM
Hi, the way I did it (also didnt want to use PlayerPrefs to save stuff) was by using the BinaryWriter/BinaryReader of the "System.IO" package. This way I could hide the file wherever I wanted, and since only you know wich bit refears to wich piece of information (CheatEngine and whatnot can still figure this out), I felt somewhat safe about cheaters. But if you really want it to be cheatsafe then you can´t save it locally either way.
I think around that yes.
Actually i also using Gamesparks as Cloud to save the Highscore and the Brick Count so my idea now is i keep the Scriptable Object but when u start the Game i just load all Data stored from the Cloud and insert it into the Scriptable Object then its not Zero anymore and you have the actual data saved on a cloud for the player.
I think this will be now the easiest way to handle this. To Prevent players from Cheating and having a easy life as Coder :p
One of the reason why i wanted to make that, is that i wanted the player can play Offline and when u come Online and start Playing he automatic set ur scores u made offline to the Cloud.
Your answer
Follow this Question
Related Questions
Make ScriptableObject assets customizable. 1 Answer
Reference to scene objects from scriptable objects 1 Answer
ScriptableObject reference resetted after a method is finished. 2 Answers
Some data of scriptable object resets when Unity Editor restarts 0 Answers
Instantiate ScriptableObject with a random asset from List 1 Answer