Question any advice or suggestions optimizing the code?

Bl4ugr4n4

New member
Joined
Jun 1, 2022
Messages
1
Programming Experience
Beginner
C#:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class HUD : MonoBehaviour
{
    //
    public Timer timerScript;
    public GameObject panel;
    public int  finalCalc;
    public TMP_Text message;
    public TMP_Text finalScore;
    public int       score = 67;
    public TMP_Text scoreText;
    public int keys = 3;
    public int keysToCollect = 5;
    public TMP_Text keysText;
    public int lives = 5;
    public TMP_Text livesText;


    // Start is called before the first frame update
    void Start()
    {
        scoreText.text = "Score:" + score.ToString();
        keysText.text = "Keys:" + keys.ToString() + "/" + keysToCollect.ToString();
        livesText.text = "Lives:" + lives.ToString();

    }

    // Update is called once per frame
    void Update()
    {
        scoreText.text = "Score:" + score.ToString();
        keysText.text = "Keys:" + keys.ToString() + "/" + keysToCollect.ToString();
        livesText.text = "Lives:" + lives.ToString();

        if (lives <1)
        {
            EndGame();
        }
     
    }
    public void EndGame ()
    {
        if (lives <1)
        {
            Debug.Log("sorry");
            panel.SetActive(true);
            message.text = "Sorry you lost, try again";
            finalScore.text = score.ToString();
             Cursor.lockState = CursorLockMode.None;
             Time.timeScale = 0;
             if (score > PlayerPrefs.GetInt("HighScore", 0))
             {
                 PlayerPrefs.SetInt("HighScore", score);
             }
        }
        else
        {
            Debug.Log("You Won");
            panel.SetActive(true);
            message.text = "Well Done you won!";
            finalScore.text = score.ToString();
            finalCalc = score * Mathf.RoundToInt(timerScript.timeRemaining);
            finalScore.text = finalCalc.ToString();
             Cursor.lockState = CursorLockMode.None;
             Time.timeScale = 0;
             if (finalCalc > PlayerPrefs.GetInt("HighScore", 0))
             {
                 PlayerPrefs.SetInt("HighScore", finalCalc);
             }
            
        } 
    }
}
 
Last edited by a moderator:
You could instantiate an interface, create a method to initialize all your types, and use string interpolation. That's just on the face of it...
 
The class variables should not be public. The ones that should be exposed should be wrapped in public properties.

Some of the class variables seem to only be used in local contexts. Perhaps they should be local variables instead of class variables.

Indentation and use of whitespace (both vertical and horizontal) seems to be inconsistent.
 
Also, isn't your code sort of mixing up responsibilities. Presumably the HUD class is responsible for updating the HUD. But why is it also responsible for updating the high score value?
 
Back
Top Bottom