How to save data between Scenes in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
when making games in unity very often we need to persist some data in between scenes for example in my test project i need to persist the score of our player so that i can show it in the end scene hi i'm peter and welcome to sunny valley studio tutorials there are many ways to persist data in unity and especially between scenes and one of those mostly suggested is player preps that can store string float and integer values into user platform registry this is a simplest solution so let me show you how it works i have a public class score system that has string score key called score now this key will be assigned to the value that i want to store and i have a public int current score property which i will assign the score to and i can call private void awake in the awake i can read current score equals playerprefs.getint which gets us an integer through a key if you pass a key score if it doesn't exist it will return default value for the integer so 0 if it exists is reads the score so the benefit is that player prefs can store data in between game sessions not only in between scenes and to set the score i have a public void seat score i pass in score to it and i call player pref's set int score key and i assign score to it just to show you how it would be used i have in one script the reference to my ui so tmp text reference and in score and score system score system reference now in the start i get the score system using find object of type i assign to my int score value the score system current score and i store the score inside my script here and i set the ui text to be score plus the empty string now whenever i want to add to the score i call score plus equals hundred and next i set score text so the ui to be the score and when i want to save my score i simply call at the end scored system.set score and i assign the current score value to be saved in my database or on my hard drive while this solution seems perfect there are some downsides it is not very reliable when we have a webgl build i certainly had a lot of issues with this but also when we are persisting data between game sessions this can lead to some problems like you are reading the old score into your game because you forgot to reset it so usually in our main menu scene we need to have something like reset game values class that will find the object that we want to reset and set the score to be 0 before we started a new game session another problem with this solution is that the score keeper needs to be accessible for all your scenes in my case i am adding additively a new scene to my game general scene and it will get the reference to the score keeper that is placed in my general scene and when i started the game again the level one accesses the score keeper so i believe there is a much better way to this and i would suggest that we use scriptable objects instead so what we can do is create a new folder in our scripts let's call it so data and what we can do is open it up create a new c-sharp script let's call it float so and let's open the script up what we can do here is delete everything inside this class and instead of mono behavior we can type scriptable object now this will be a scriptable object and to create it from our create menu inside our inspector we need to create create asset menu now in this class what i want to type is prop full if you are using visual studio you can double tap on it click tab twice and it should be able to create this property with a backing field private into my var what we need to do is change this to be a float value and we are going to change the property also to be float value i will call my value underscore value and i will call the property value and this way we can set the value inside our float scriptable object as well as from other scripts that reference the scriptable object now to show this value in the inspector to serialize it i will call add here serialized field and now i will be able to preview the score value from the level of my scriptable object reference now let's save this let's go back to unity now if we go back to unity what we can do is right click create a float so value let's call it scar so now we can set the value here but the most important thing is that it exists inside our project as an asset and unity can load it into the memory of our game whenever we have a reference to a scriptable object in our scene so now we do not need this general scorekeeper object inside our scene what we can do is open the scripts that will use this so in my case this is an enemy spawner script and instead of having the reference to my score system i can create a reference to my private float so value score so now i do not need to get the reference to my score system because i can assign this through the inspector and now we also do not need to have the score in value in our enemy spawner or any class that uses the score because now i can use cortex so the ui dot text call and set it to be score so dot value the same way we can slide it down to find when i am adding to the score and i can add to the score saved in my scriptable object and again update the ui with this value and now this will be persisted across a multiple scenes as long as of course the scriptable object is in the memory of my project so i do not need to explicitly call set score and save it in my game when i want to exit to another scene so now all i need to do is go back to my game view i need to open my scene where i have the reference to my enemy spawner that takes into account that we want to save the data inside our float value here it is the enemy spawner script and i need to assign here score float i need to select my score so a reference now again as i have mentioned this score so scriptable object will be loaded into the memory of unity but when we move from level 1 to level two it can be unloaded unless we have a reference to it in the scene as well so we need to ensure that our scenes that uses this needs to have an object referencing the score so otherwise we are going to lose the data saved in it so now last scene my end screen scene needs to have the end result and you can see we do not have the reference here so i need to open the script and instead of finding the object score system i need to create a serialized field private again this was float so score so and i can get the reference to it from the project and i can assign score so dot value to show my score at the end in my game so again now i can assign my score so to this and now if i start my game again let me save this we are going to see that we are going to have the score set to be zero and i can update it by killing enemies and if i go to the next scene we are going to see that the score is persisted as well as we can select our i think scripts so data score so and i can see the value preview it from the level of our scriptable object and the great thing about it is that if i want to decouple my enemy spawner sounds the my enemy spawner shouldn't have the reference to my ui element to update this cortex we can assign our score so score so value to a separate class that will update our ui now this solution was taken from unity talk from 2017 i will post the link to it in the description now one more issue that we need to address is that while our garbage collector may unload the scriptable object from the memory of our game we may want to do it manually just to ensure that our reset game values will reset our score so again all we need to do is instead of finding the object we are going to create serialized field private reference to our scriptable object so this was float so scar so and what we can do is set the score so dot value to be 0 when we want to start our game and reset the game values while the one drawback of the using scriptural object solution is that we are not saving the score onto the hard drive we can of course use the player prefs to do that but right now what we have achieved is the decoupled code so our classes don't have to know about each other all they need to know is that there is scriptable object in our project and there is no need for a manager class like a scorekeeper that is persisted across all the scenes so that they can access it we can access the scriptable object and aside in front from the level of the project window i would not have to have the score keeper inside our scenes now if you want to learn more about making 2d games in unity and to improve your coding skills check out my video courses the link will be in the description of this video see you in the next tutorial
Info
Channel: Sunny Valley Studio
Views: 30,005
Rating: undefined out of 5
Keywords: How to save data between Scenes in Unity, unity playerprefs, unity scriptable objects, unity persist data between scenes, unity save data between scenes, how to save score in unity, how to keep data between scenes unity, keep score between scenes unity, unity saving data, unity tutorial saving data, unity tutorial persisting data, unity tutorial saving score
Id: MBM_4zrQHao
Channel Id: undefined
Length: 9min 44sec (584 seconds)
Published: Tue Feb 01 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.