Click here to Skip to main content
15,868,164 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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);
             }
              
        }   
    }
}


What I have tried:

I need help optimising this code, if anyone has any ideas please dont hesitate to say, thank you
Posted
Updated 1-Jun-22 4:59am
v2
Comments
Patrice T 1-Jun-22 9:02am    
What kind of optimization are you looking for ?
There is not much to get rod off.
gggustafson 1-Jun-22 10:37am    
1. Invoke "Start" the beginning of "Update" after removing duplicate code from "Update"
2. Move "panel.SetActive ( true );" to before the if statement
3. Remove "finalScore.text = score.ToString ( );" from the else block
4. Move "Cursor.lockState = CursorLockMode.None;" and "Time.timeScale = 0;" to following the if statement

You should format your code (alphabetize variable declarations, comment each method, etc.) to make it easier on following programmers.

1 solution

Try this
C#
public void EndGame ()
{
    if (lives <1)
    {
        message.text = "Sorry you lost, try again";
    }
    else
    {
        message.text = "Well Done you won!";
        score = score * Mathf.RoundToInt(timerScript.timeRemaining);
    }
    Debug.Log(message.text);
    panel.SetActive(true);
    finalScore.text = score.ToString();
    Cursor.lockState = CursorLockMode.None;
    Time.timeScale = 0;
    if (score > PlayerPrefs.GetInt("HighScore", 0))
    {
        PlayerPrefs.SetInt("HighScore", score);
    }

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900