Hello guys! I am a student and I am new to C# and am currently making my second game using this language.
I want to print out a text where the text is the score and the score is increasing as the time goes by, and I have been figuring out what is the problem with the code.
Thanks,
K
<Preformatted text
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class ScoreManager : MonoBehaviour
{
#region Singleton
public static ScoreManager Instance;
[SerializeField] private TextMeshProUGUI scoreUI;
private void Awake()
{
if (Instance == null) Instance = this;
}
#endregion
public float currentScore = 0f;
public bool isPlaying = false;
private void Update()
{
if (isPlaying)
{
currentScore += Time.deltaTime;
}
if (Input.GetKey(KeyCode.Space))
{
isPlaying = true;
}
}
public void GameOver()
{
currentScore = 0f;
}
public string PrettyScore ()
{
return Mathf.RoundToInt(currentScore).ToString();
}
ScoreManager sm;
private void Start()
{
sm = ScoreManager.Instance;
}
private void OnUGUI()
{
scoreUI.text = sm.PrettyScore();
}
}