i need in main menu scene score texts shows overal gathered scores of all sessions.
but i need ui text score in gameplay scenes only runs between 0 and max scores that user gets.
i have already implemented the main menu and saving via playerprefs , but still i cannot in other scenes have score texts between 0 and max scores which is affordable.
…but then, the code you show never uses PlayerPrefs at all.
You were quite right: PlayerPrefs can save the score. Try using it, and if you run into troubles, post your attempt and any error messages you got and we’ll see if we can help.
Either delete the PlayerPrefs key every time the user starts a new game or set the PlayerPref key value to 0.
//Delete the score value, assuming it's key is "Highscore"
PlayerPrefs.DeleteKey("Highscore");
//Alternatively, just set it to 0
PlayerPrefs.SetInt("Highscore", 0);
Here’s a good tutorial about using PlayerPrefs for high scores:
i could not handle it when it comes to 2 variables as score and highscore.
my game was correct until i wanted to reset in every game variable score and pass its current amount to highscore after each time game is played . but i lost all connections. here are classes : how can i save total score in main menu scene, and i show score in game play scene . score should be reset each time . it means it start from 0.
now here i want to see total amount of scores of all sessions :
public class MainmenuLevelSelectionScores : MonoBehaviour
{
void Start ()
{
ScoreManager.instance.scoreText = GetComponent<Text>();
ScoreManager.instance.scoreText.text = PlayerPrefs.GetInt ("TotalScores").ToString();
}
}
it shows total scores . can someone give me ideas how when i go back to game play scene and gather some more scores there , after coming back to here , to see them added here too?
there is score and highscore int in gamecontroller.
there is a text in main menu and a text in game play scene.
what i need is to show total scores in main menu scene .
in gameplay scene i need each time to begin from 0 . i use playerprefs;
now each time i go to gameplay scene , score starts from 0 but when i return back to main menu scene , the new score will be overwritten on last score.
instead i need new amount to be added to that in main menu .thanks.
void GotPoints(int val) { // don't even need 'val' if you can only ever get 1.
gameScore += val;
highScore += val;
}
Normally when I think of high score, I think : if score > highscore, highscore = score; However, your wording makes it sound like maybe you mean accumulated score … which is what my post tries to answer above.
Still not working.
I have playerpprefs.setint total score .
In main menu this int will be got via playerpprefs.getint .
But where my score is saved i need to say to add it to highscore , not the highest score ever gathered . I need to sum amount of all times of user scores .
Are you talking about persisting data between scenes (DontDestroyOnLoad) or persisting data between actual sessions (users players, gets XP to 1,000, closes game. Comes back 1 hour later and you want to set their XP back to 1000) (?)
if its the latter, its something like this:
using UnityEngine;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class CalendarData : MonoBehaviour {
public int GameYear;
public int GameMonth;
public int GameDay;
private void Start()
{
// if CalendarData.dat exists - load it.
if (File.Exists(Application.persistentDataPath + "/CalendarData.dat") == true)
{
Load();
}
}
// Load Data
public void Load()
{
if(File.Exists(Application.persistentDataPath + "/CalendarData.dat"))
{
// create new binary formatter
BinaryFormatter CalendarDataBF = new BinaryFormatter();
// open file
FileStream CalendarDataFS = File.Open(Application.persistentDataPath + "/CalendarData.dat", FileMode.Open);
// deserialize data
CalendarDataContainer calendarData = (CalendarDataContainer)CalendarDataBF.Deserialize(CalendarDataFS);
// close file
CalendarDataFS.Close();
// data to load
GameYear = calendarData.GameYear;
GameMonth = calendarData.GameMonth;
GameDay = calendarData.GameDay;
}
}
// Save Data
public void Save()
{
// create new binary formatter
BinaryFormatter CalendarDataBF = new BinaryFormatter();
// create new filestream
FileStream CalendarDataFS = File.Create(Application.persistentDataPath + "/CalendarData.dat");
// data to save
CalendarDataContainer calendarData = new CalendarDataContainer();
calendarData.GameYear = GameYear;
calendarData.GameMonth = GameMonth;
calendarData.GameDay = GameDay;
// serialize data
CalendarDataBF.Serialize(CalendarDataFS, calendarData);
// close file
CalendarDataFS.Close();
}
}
// serializable data container class
[Serializable]
class CalendarDataContainer
{
public int GameYear;
public int GameMonth;
public int GameDay;
}
This tutorial will show you what you need to know in order to persist data between sessions.
Application.persistentDataPath is a path on the users machine (works on everything like android/ios, pc, mac etc, but not the web player)
it stores a .dat file which can be saved to and loaded from. You use a serializable class to make a data container (which contains the data you want to save/load)
if you make the Load() and Save() functions public, you can call them from other scripts when you want to save the data.
Goodness gracious! Don’t do all that. The OP is having a hard enough time using PlayerPrefs; let’s not introduce him to file I/O (which certainly works, but is a lot more complicated than PlayerPrefs for something like saving a score).
Yeah winterflux while everything you said is technically correct, it’s more likely to confuse the OP than to help. Also your post kinda implies that PlayerPrefs can only persist data between scenes and not sessions, which is incorrect. The main purpose of PlayerPrefs is to persist small amounts of data between sessions, like high scores or settings
Guys please dont make it too much complex . i have already a variable int score which is saved to key “totalscore” with playerprefs.setint.
now i added a new variable in my gameplay script too as public static int highscore.
and then highscore += score ;
but , i dont know how before leaving game scene add that new amount of score to highscore.
so that in menu scene my ui text reads it amount from highscore variable .
can you give me idea how :
add score int to another int variable at the end of game. accumulation
True, but I was unsure what OP wanted (so I tried to provide a Copy/Paste script with comments).
I didn’t mean to suggest PlayerPrefs can not be used to do that, so thanks for pointing it out. That’s a good distinction to make and useful for anyone reading who is new to Unity and reads this thread.
Although, I will say they should only be used to store the players preferences (so the HighScore is not a player preference). I mean, in a single player game, serializing data might be over-the-top so I guess it’s down to personal preference.
I apologize if this is too simple and doesn’t address your question, but I’m having trouble understanding what you’re asking. Can’t you just put this code into whatever script is storing the score:
are you trying to write the score to a UI text box?
something like this
// need this or it will not work
using UnityEngine.UI
// This is the UI component attached to the cavas and used to display the score
public Text Score;
// this is the players score
public int PlayerScore;
void Update()
{
PlayerScore += 1;
UpdateScore();
}
void UpdateScore()
{
// set the Score UI to display the PlayerScore
Score.text = "" + PlayerScore;
}
That might help (ironically rated more difficult than the persistent data tutorial I linked)
OP wants to display the total combined score of all sessions on the main menu
OP wants to display the “current score” in the game play screens, where they can only go to a maximum number
To update a UI text element you need to reference the UI element in your Menu script
Create a public reference to the UI text of the Menu. Drag the Text element from the hierarchy to the empty slot in the inspector for the object this script is attached to.
public Text TotalScore;
You will need to reference the UI stuff for it to work.
using UnityEngine.UI
You then need to set the text component of the Text UI with the score stored in player prefs. (this is just an example)
public int totalScore;
void Start()
{
TotalScore.text = "" + totalScore;
}
During GamePlay scenes, you will need something like a max score
public int MaxScore;
public Text PlayerScore;
public int playerScore;
void ExampleMethod()
{
// if the player score is less than max score,
// continue increasing player score and
// update UI score displayed to user
if (playerScore < MaxScore)
{
playerScore += 1;
PlayerScore.text = "" + playerScore;
}
else
{
QuitScene();
}
}
void QuitScene()
{
}
i dont know if() statement which you said about how will add its amount to highscore and save it .
can you explain more there? only here i need some ideas. the other parts are done.