Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more..
    Dismiss Notice
  3. Dismiss Notice

Local Highscore Page C#

Discussion in 'Scripting' started by ashkanaral, Mar 5, 2020.

  1. ashkanaral

    ashkanaral

    Joined:
    Sep 3, 2018
    Posts:
    22
    Trying to make a local highscore page in C#. It does not work. The public Text and Int probably have to be the same. This is so far the only way I know how to make it unless there is another way.

    Here is the code.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class HighscoreBoard : MonoBehaviour
    7. {
    8.     public Text[] scoreArr;
    9.     public int[] scoreArrNum;
    10.  
    11.     int newScore = 0;
    12.     int ppScore = PlayerPrefs.GetInt("HighScore");
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.    
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         CheckForHighScore(ppScore);
    24.     }
    25.     public void CheckForHighScore(int value)
    26.     {
    27.         int newScore = value;
    28.         int oldScore = 0;
    29.         for (int i = 0; i < scoreArrNum.Length; i++)
    30.         {
    31.             if (scoreArrNum[i] < newScore)
    32.             {
    33.                 oldScore = scoreArrNum[i];
    34.                 scoreArrNum[i] = newScore;
    35.                 newScore = oldScore;
    36.                 scoreArr[i].text = newScore.ToString();
    37.             }
    38.         }
    39.     }
    40. }
    41.  
     
    Last edited: Mar 5, 2020
  2. wileyjerkins

    wileyjerkins

    Joined:
    Oct 13, 2017
    Posts:
    77
    What is not working? That helps us figure out what is going wrong.
    In general, you should not be checking the high score in an Update loop. That is a waste of resources. Put it on the player OnDisable is how I usually do it. If (HP <= ){ CheckForHighScore(); } ... something like that.
    How many high scores are there? I'd store those in a serialized string separated by commas instead of using 1 int like you are doing here. The parse those out, recast as ints/floats and do the comparison/replace/concat of string and resave to player prefs and make your list for the player to admire.
    Also, you should only use arrays when the values in the array will not change. Use lists when the values will change. They are faster and easier to work with and designed to do exactly what this problem is all about. Example: I have a separate array for each type of enemy that can spawn/clone in my game. The blueprint for that enemy will not change in runtime, so it makes sense to use an array. The cloned copies I keep in a list because they WILL change (they may get damaged, change weapons, colors, etc). A list of high scores is guaranteed to change at least once! Therefore use a list.
     
    ashkanaral likes this.
  3. ashkanaral

    ashkanaral

    Joined:
    Sep 3, 2018
    Posts:
    22
    Thanks for the reply. Do you think it is because I do not have highscore saved? I have the playerprefs keeping score just not a score saved for highscore. How would I code saved highscore and not a score tracker?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    Break the problem up, don't try and wadd it all together like this. Having written about 389,000 high score charts in my gaming career, here are the key parts:

    - a list of "entries" controller that tracks from 0 to N items (this is the only persistent state)
    - a sorting mechanism that sorts for best, whatever that means (lowest time, highest amount)
    - mechanisms to remove and add new scores
    - mechanisms to save and load the existing state of scores
    - a display mechanism that displays that list of entries (you might have one for just the high score, another for all of them in a full page display)

    When you break those concepts apart it becomes a lot easier to make each little chunk and prove it works.
     
    Joe-Censored likes this.