Reputation: 127
please tell me the answers to my questions. for example, I have a lives system and a timer, I made a mistake at some level and my life was taken away, then the timer will turn on.
Questions:
Upvotes: 0
Views: 86
Reputation: 2306
There are many difference ways to persist data across scenes.
Some of the other responses here have pointed out Singleton and PlayerPrefs as potential ways to do this. I'll provide you with a list of options that I have used, and when I personally find them most useful.
DontDestroyOnLoad(this)
to a game object allows a GameObject to live across multiple scenes without running the risk of creating duplicate instances.Saving to system storage or cloud
For your questions:
how to save the number of lives by moving to another scene and so that the timer is not interrupted while it is working? A class that has had DontDestroyOnLoad(this)
appplied would work well to pass the number of lives from scene to scene. It is often recommended to make this class a Singleton to avoid multiple instances of this class
if the user left the game and logged in a minute later, then how can you make the timer with the life system work while the user is not there? If you want time to progress while the game is not running, you'll need to rely on DateTime and simulate the progress of your game. If you want to prevent the user from cheating by manipulating their system clock, you'll likely need to look into a server-based time check.
Also with coins, how to save the number of coins when passing through the scenes and when exiting the game? If your data is more complex than a few values, SQLite or JSON is likely the go-to solution. If it's just a 1 or 2 values, then PlayerPrefs is usually fine. If you want to add some cheat prevention, you'll likely want to utilize a cloud-based solution.
Upvotes: 3
Reputation: 111
I would use PlayerPrefs class. It allows you storing float, integer and string type variables.
# Set lives left
PlayerPrefs.SetInt("Lives", 3);
# Get number of lives left
var lives = PlayerPrefs.GetInt("Lives");
Upvotes: 1
Reputation: 338
The answer to your 1st and 3rd question is a Singleton Class. The answer to your 2nd question is saving the logout time in a variable and comparing it later when the user logs in again.
TimeElapsed = CurrentTime - PreviouslySavedTime
Upvotes: 1