- Home /
how to set a score from 2 individual game.
Now i have 2 individual game in a project, this 2 game have its own score system.
i have a main menu for this 2 game, this main menu have 2 button for entering the game and 2 text for high score and current score.
High score is change the lowest score to the highest score from the 2 game. example game A score is 5 and game B score is 10, so game B score will replace the oldest score. vice versa. it will only can increase.
current score is change the score every time back to main menu. it dun care which score bigger, just want the current score of the game that was playing. it will change every time either the score is big or small.
now i would like to ask how to set the current score?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using StrikeFree.Game;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Runtime.Serialization.Formatters.Binary;
using System;
using System.IO;
using CodeStage.AntiCheat.ObscuredTypes;
public class HighScore : MonoBehaviour {
//ScoreManager.score.ToString();
public Text HighScoreText;
public Text CurrentScoreText;
static public int highScore;
static public int currentScore;
int tempScore = 0;
void Awake()
{
Load ();
//ObscuredPrefs.DeleteAll ();
}
void Update(){
HighScoreDisplay ();
CurrentScoreDisplay ();
if (ObscuredPrefs.HasKey ("highScore")) {
highScore = ObscuredPrefs.GetInt ("highScore");
}
if (tempScore < Score.score && highScore < Score.score) {
tempScore = Score.score;
highScore = tempScore;
Save ();
} else if (tempScore < ScoreManager.score && highScore < ScoreManager.score) {
tempScore = ScoreManager.score;
highScore = tempScore;
Save ();
}
}
void HighScoreDisplay () {
HighScoreText.text = "High Score: " + highScore;
}
void CurrentScoreDisplay()
{
CurrentScoreText.text = "Current Score: " + currentScore;
}
public void Save()
{
CodeStage.AntiCheat.ObscuredTypes.ObscuredPrefs.SetInt ("highScore", highScore);
CodeStage.AntiCheat.ObscuredTypes.ObscuredPrefs.Save ();
}
public void Load()
{
highScore = CodeStage.AntiCheat.ObscuredTypes.ObscuredPrefs.GetInt ("highScore");
}
}
Answer by UnityCoach · Mar 20, 2018 at 08:42 AM
Well, since this is present in your scene, you can simply add a prefix to all game specific prefs.
[SerializeField] string prefix;
highScore = PlayerPrefs.GetInt (prefix+"highScore");
Then you set the prefix to a different key in every game.
can i know how basically u mean and how to set it? and i wan to set the current score and current score are not going to save inside PlayerPrefs, it will only going to take from the game after u go back to main menu.
Well, I guessed that current score isn't a problem, as it's probably reset every time to start the game. But you can use the same technique. Adding a prefix to your key so that every score is different.
prefix = GameA_ GetInt (prefix+"HighScore")
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
score counter is not accurate 1 Answer
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer