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: