Hello, guys! I have a small issue in my code. For one reason, it says that I have null reference on my best time text object but I am pretty sure it is assigned. Check my code here for more information and console:
GameManager.cs script
// Check for null bestTimeText
if (bestTimeText == null)
{
Debug.LogError("bestTimeText is null in Awake!");
}
else
{
LoadBestTimeOnStartGame();
UpdateBestTime(bestTime);
}
and
public void UpdateTimer()
{
if (timerText != null)
{
time += Time.deltaTime;
int minutes = Mathf.FloorToInt(time / 60);
int seconds = Mathf.FloorToInt(time % 60);
timerText.text = string.Format("{0:00}:{1:00}", minutes, seconds);
timerText.text = "Timer: " + timerText.text;
Debug.Log("Timer updates: " + timerText.text);
}
}
and
public void CheckSaveBestTime()
{
if (time > bestTime)
{
bestTime = (int)time;
PlayerPrefs.SetInt("BestTime", bestTime);
PlayerPrefs.Save();
Debug.Log("Best time is: " + bestTime);
Debug.Log("Checked and saved best score!");
UpdateBestTime(bestTime);
}
bestTimeText.text = bestTime.ToString();
}
and
public void UpdateBestTime(int bestTime)
{
if (bestTimeText != null)
{
bestTimeText.text = "Best Time: " + bestTime;
Debug.Log("Best time updates: " + bestTime);
}
else
{
Debug.LogError("bestTimeText is null in UpdateBestTime!");
}
}
The errors occur when I press play button and enters the main scene of my game.