Home > Blockchain >  How do I save values when going through other scenes?
How do I save values when going through other scenes?

Time:05-23

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:

  1. how to save the number of lives by moving to another scene and so that the timer is not interrupted while it is working?
  2. 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?
  3. Also with coins, how to save the number of coins when passing through the scenes and when exiting the game?

CodePudding user response:

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

CodePudding user response:

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");
  • Related