Create highscore value - c#

I want to make a highscore marker, but it keeps its value always in 0. Help please
using UnityEngine;
using UnityEngine.UI;
public class score : MonoBehaviour {
public Transform jugador;
public Text scoreText;
public Text highScore;
void Start(){
highScore.text = PlayerPrefs.GetString ("HighScore", 0);
}
void Update () {
scoreText.text = jugador.position.z.ToString("0");
}
public void HighScore(){
PlayerPrefs.SetString("HighScore", jugador.position.z.ToString("0");
}
}

Setting Score:
public Text score;
public Text highscore;
private float currentscore = 0;
To increase the score we can simply set the currentscore to the current position.z and then update our Text Component.
Increasing Score:
private void Update () {
// Get the current Z position of jugador and round to a whole number.
currentScore = jugador.position.z;
// Set our scoreText Component to the currentScore.
scoreText.text = currentScore.ToString("0");
}
After that we set the Highscore to the currentscore with the SetHighScore() Method in our Score.
Set HighScore:
private void SetHighScore() {
// Set HighScore to the currentScore if it's bigger than the current HighScore.
if (currentScore > PlayerPrefs.GetFloat("HighScore", 0.0f)) {
PlayerPrefs.SetFloat("HighScore", currentScore);
}
}
To actually see our new score when we died. We can use the Start() Method to set the highscore Text.
Set HighScore Text:
void Start() {
// Set highscore Text to the current HighScore.
highscore.text = PlayerPrefs.GetFloat("HighScore", 0.0f).ToString("0");
}

Related

When I add a score, the scoreText just rapidly displays the scores added to itself

I am a newbie and I am making this android game that if the player has destroyed a meteor, a score is added to its score, well the problem is that I want to display the score in my scoreText, but whenever I initialize it in my Update(), it rapidly adds the score in my scoreText. I just cant figure out how to properly add the score to my scoreText This is my game manager script
public class GameManager : MonoBehaviour
{
public static int displayScores;
public int displayTheScore;
public Text scoreText;
// Start is called before the first frame update
void Start()
{
scoreText.text = "" + displayScores;
}
void Update(){
scoreText.text = "" + displayScores;
displayScores += Meteor.displayScore;
}
}
And this is the script to making the conditions that if the meteor is detroyed, a score is added to displayScore depending on the hits to the meteor
public class Meteor : MonoBehaviour
{
public int maxHealth;
public int currentHealth;
public float speed;
public int hits = 0;
public int score = 100;
public static int displayScore;
public int display;
public int currentHealthChecker;
public static int counter;
public Health healthBar;
public GameObject canvas;
public Transform effect;
// Start is called before the first frame update
void Start()
{
currentHealth = maxHealth;
healthBar.setMaxHealth(maxHealth);
}
// Update is called once per frame
void Update()
{
transform.Translate(Vector2.down * speed * Time.deltaTime);
}
public void OnTriggerEnter2D(Collider2D other){
if(other.transform.tag == "bullet"){
hits++;
canvas.SetActive(true);
currentHealth--;
currentHealthChecker = currentHealth;
healthBar.setHealth(currentHealth);
display = displayScore;
if(currentHealth <= 0){
displayScore = score * hits;
Instantiate(effect, other.transform.position, other.transform.rotation);
Destroy(this.gameObject);
counter++;
canvas.SetActive(false);
}
Destroy(other.gameObject);
}
if(other.transform.tag == "bottom"){
Destroy(this.gameObject);
}
}
}
you are initializing in update method, that why it keeps adding rapidly because update is called every frame. instead of adding in update, add the score value in ontriggerenter2d method.
public void OnTriggerEnter2D(Collider2D other){
if(other.transform.tag == "bullet"){//add score}

Unity: On Touch set score to zero

I want to set the a score to zero whenever a certain button is pressed.
This is the script I made:
public class zeroOut : MonoBehaviour
{
private Rigidbody2D rb;
void OnMouseDown()
{
scoreScript.scoreValue -= scoreScript.scoreValue;
}
}
This is the scoreScript:
public class scoreScript : MonoBehaviour
{
public static int scoreValue = 0;
Text score;
// Start is called before the first frame update
void Start()
{
score = GetComponent<Text> ();
}
// Update is called once per frame
void Update()
{
score.text = "" + scoreValue;
}
}
Have you got any suggestions?

After Game Over how to stop score from increasing

Im making a endless runner game and am using a 'ScoreManager' object with a box collider 2d set to 'is trigger' increasing the score every time a object hits it. But I want it to stop increasing the score if the health reaches 0. This is the ScoreManager code:
public int score;
public Text scoreDisplay;
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Obstacle"))
{
score++;
}
}
private void Update()
{
scoreDisplay.text = score.ToString();
}
I would like to add a:
public int health = 3;
and in the Update function:
if (health <= 0) {
Destroy(gameObject);
}
But that doesn't seem to work.
The health is displayed in a player script.
public class Player : MonoBehaviour
{
private Vector2 targetPos;
public float Yincrement;
public float speed;
public float maxHeight;
public float minHeight;
public Text healthDisplay;
public GameObject gameOver;
public int health = 3;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
private void Update()
{
healthDisplay.text = health.ToString();
if (health <= 0) {
gameOver.SetActive(true);
Destroy(gameObject);
}
Any thoughts?
Wherever your health is defined, substitute it by a property that launches an event whenever set to a value < 0. Like this:
public class Player : MonoBehavior
{
public delegate void PlayerDiedDelegate();
public static event PlayerDiedDelegate onPlayerDied;
private int _health;
public int health
{
get
{
return _health;
}
set
{
_health = value;
if(_health < 0)
{
onPlayerDied?.Invoke();
}
}
}
}
Now you can attach any controller in your scene to the event:
public class ScoreController : MonoBehavior
{
private void Awake()
{
Player.onPlayerDied += OnPlayerDied;
}
private void OnPlayerDied()
{
// Put your logic here: stop collecting score etc.
}
}

How do I get the score to reset

Once the player dies and starts the game, the score is not resetting and staying as the previous score. I would like the score to reset once the player dies or leaves the game. How do you do this?
public class ScoreScript : MonoBehaviour {
public static int scoreValue = 0;
Text score;
// Use this for initialization
void Start () {
score = GetComponent<Text>();
}
// Update is called once per frame
void Update () {
score.text = "Score: " + scoreValue;
}
}
score.text is storing the evaluated value of Score: and whatever is stored in scoreValue at the time Update() is called. None of your code ever updates scoreValue from what you've shown.
Also be aware of scoreValue being static, be sure that accurately reflects your intention (e.g. is scoreValue a property of the ScoreScript class or each instance of it?). Note that score is not static, and I would expect both of those to have the same behavior (either both static or not).
i.e. something like
public class ScoreScript : MonoBehaviour {
public static int scoreValue = 0;
Text score;
// Use this for initialization
void Start () {
score = GetComponent<Text>();
}
// Update is called once per frame
void Update () {
score.text = "Score: " + scoreValue;
}
void ScoreReset() {
scoreValue = 0;
}
void AddPoints(int points) {
scoreValue += points;
}
}
If you load the scene after player die scoreValue will be 0 cause as I see that you assign 0 in your above code.

Saving and loading high scores in Unity2d

What I've done so far is set a score to increase by each second in gameplay, get the score to show within the game scene and then set the highscore to be equal to the score if the score is greater than the highscore. This is my code so far:
bool gameOver;
public Text scoreText;
public Text highScoreText;
int score;
int highScore;
// Use this for initialization
void Start () {
score = 0;
highScore = 0;
InvokeRepeating ("scoreUpdate", 1.0f, 1.0f);
gameOver = false;
}
// Update is called once per frame
void Update () {
scoreText.text = "★" + score;
highScoreText.text = "★" + highScore;
}
public void gameOverActivated() {
gameOver = true;
if (score > highScore) {
highScore = score;
}
PlayerPrefs.SetInt("score", score);
PlayerPrefs.Save();
PlayerPrefs.SetInt("highScore", highScore);
PlayerPrefs.Save();
}
void scoreUpdate() {
if (!gameOver) {
score += 1;
}} }
"game over" is equal to true when this code happens:
void OnCollisionEnter2D (Collision2D col) {
if (col.gameObject.tag == "enemyPlanet") {
ui.gameOverActivated ();
Destroy (gameObject);
Application.LoadLevel ("gameOverScene2");
}
}
What I want is at this point (when the objects collide and game over is true) for the score to be saved, then the game over scene is loaded. How do I save the score at game over, then load it in the game over scene along with a saved highscore??
There are multiple ways you can do this, two of the most obvious ways to do it if you are only persisting the score for that session is to store it in a Static Class or a Singleton. These classes will persist for however long you need them to, regardless of scene loading, so be careful how you manage the information in them.
One example of a static class implementation would be:
public static class HighScoreManager
{
public static int HighScore { get; private set; }
public static void UpdateHighScore(int value)
{
HighScore = value;
}
}
If you are looking to persist the data for a longer amount of time you will need to look at this
I hope this helps!

Categories

Resources