Search Unity

Resolved ScoreCount is resetting

Discussion in 'Scripting' started by Ch267, Sep 28, 2020.

  1. Ch267

    Ch267

    Joined:
    Sep 16, 2019
    Posts:
    55
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class ScoreManager : MonoBehaviour
    7. {
    8.     public static int scoreCount;
    9.     public static int highScoreCount;
    10.     public Text scoreText;
    11.     public GameObject zombie;
    12.     public CanvasGroup score;
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         //scoreCount = 0; // set the score to 0 at the start of the game
    17.         highScoreCount = PlayerPrefs.GetInt("HighScore", highScoreCount); // store the highScore
    18.         score.alpha = 1;
    19.         score.interactable = true;
    20.         score.blocksRaycasts = true;
    21.  
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.         if(GameObject.FindGameObjectWithTag(tag: "Zombie") == null)
    28.         {
    29.             scoreCount += scoreCount + 100;
    30.         }
    31.  
    32.         scoreText.text = "Score: " + scoreCount.ToString(); // display the player's score
    33.  
    34.         if (scoreCount > highScoreCount)
    35.         {
    36.             scoreCount = highScoreCount;
    37.             PlayerPrefs.SetInt("HighScore", highScoreCount);
    38.             PlayerPrefs.Save();
    39.         }
    40.  
    41.         if (Time.timeScale == 0)
    42.         {
    43.             score.alpha = 0;
    44.             score.interactable = false;
    45.             score.blocksRaycasts = false;
    46.         }
    47.  
    48.            
    49.  
    50.  
    51.  
    52.  
    53.     }
    54. }
    This is working for each time an object with the "Zombie" tag is destroyed, but after they are destroyed the score always resets to zero? Could anyone help me find why this is happening? Is it a loop like I'm thinking I should do, and if so which kind? And if it isn't could a Coroutine work?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Line 36 above is reversed.

    You want:

    Code (csharp):
    1. highScoreCount = scoreCount;
     
    Ch267, WarmedxMints and PraetorBlue like this.
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Double check your logic around here:
    Code (CSharp):
    1.         if (scoreCount > highScoreCount)
    2.         {
    3.             scoreCount = highScoreCount;
    4.             PlayerPrefs.SetInt("HighScore", highScoreCount);
    5.             PlayerPrefs.Save();
    6.         }
    Specifically... I think
    Code (CSharp):
    1. scoreCount = highScoreCount;
    should be
    Code (CSharp):
    1. highScoreCount = scoreCount;
     
    WarmedxMints and Kurt-Dekker like this.
  4. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    I can see a few problems here, however, your issue is that when your scoreCount is greater than the highScoreCount, you are assigning the value of highScoreCount to the scoreCount variable. I imagine you meant to do that the other way round.
     
    Kurt-Dekker likes this.
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    @WarmedxMints and @PraetorBlue I think we pulled off a legitimate trifecta this morning! WOOT!

    (I hope that is all of OP's problems too... it's certainly ONE of the problems).
     
    Ch267, JeffDUnity3D and PraetorBlue like this.
  6. Ch267

    Ch267

    Joined:
    Sep 16, 2019
    Posts:
    55
    It fixed the score count resetting but now it goes to -100 when the gameObject tagged "Zombie" and only doing it once as well rather than every time something spawns with the "Zombie" tag. Despite those problems arising and persisting respectively, thank you for pointing out my mistake on line 36, there's only so much a solo developer can do without help from Unity's awesome community!
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Rather than spritzling PlayerPrefs calls all over, you could use a construct like this:

    Here's an example of simple persistent settings using PlayerPrefs:

    https://pastebin.com/icJSq5zC

    Useful for a relatively small number of simple settings.