I have a weird issue which I can’t quite work out. The code works but I get a NullReferenceException when it is running, but as I say it is working.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Countdown : MonoBehaviour {
public float seconds = 59;
public float minutes = 0;
public Text countdownTimer;
// Update is called once per frame
void Update()
{
if (ManGame.GameHasStarted)
{
if (seconds <= 0)
{
seconds = 59;
if (minutes >= 1)
{
minutes--;
}
else
{
seconds = 0;
minutes = 0;
}
}
else
{
seconds -= Time.deltaTime;
}
if (Mathf.Round(seconds) <= 9)
{
countdownTimer.text = minutes.ToString() + ":0" + seconds.ToString("f1");
}
else
{
countdownTimer.text = minutes.ToString() + ":" + seconds.ToString("f1");
}
}
}
}
It is throwing the exception on the two lines countdownTimer.text = etc.(lines 37 and 41) Everything has been entered or dragged into the public variables in the editor. As I say it works, but I still get the errors. Any insights?
I have not actively destroyed it. This is the only script in which it is referenced. It does have a game object assigned to it in the Editor. Just a text field on a canvas. And it works. It does count down in the game.
Perhaps it is destroyed due to a scene change. Either put a breakpoint there and inspect the variable, or use Debug.Log to check if it is null (or when it becomes null).
Might be a bug in Unity. Change countdownTimer to private and save you script. Go back to Unity so it compiles the scripts and then go back and change it to public again. Back to Unity and drag the textbox over into the inspector again.
According to the debugs it is never created. As you can see I have attached it in the inspector. @WarmedxMints_1 , I tried your suggestion and no change.
Check if you have another Countdown in your scene. A nullref on a line that “works” usually means that you’ve assigned the correct object on the script you’re looking at, but there’s another copy of the script around with no reference.
You can search for scripts by type in the hierarchy by writing “t:Type” in the search bar on top. So for you, “t:Countdown”
Than I would tag the textbox and use getcomponent to find it in the start method instead or check you don’t have more than one of that script in the scene.
Also, I made a countdown timer for someone the other day which is a bit less code if you wanted to use it
using System;
using UnityEngine;
public class Countdown : MonoBehaviour
{
public float CountdownTimerInSeconds = 30f;
public float resetTimerToXSeconds = 50f;
public void Update()
{
if(Input.GetKeyDown(KeyCode.Space))
{
ResetTimer(resetTimerToXSeconds);
}
CountdownTimerInSeconds -= Time.deltaTime;
}
private void OnGUI()
{
GUI.Label(new Rect(10, 10, 150, 50), FloatToTime(CountdownTimerInSeconds));
}
private void ResetTimer(float newTimeInSeconds)
{
CountdownTimerInSeconds = newTimeInSeconds;
}
private string FloatToTime(float seconds)
{
var span = TimeSpan.FromSeconds(seconds);
return string.Format("{0}:{1:00}:{2:00}", span.Minutes, span.Seconds, span.Milliseconds);
}
}
@WarmedxMints_1 Thanks for the code. I will use it. @Baste You nailed it.I changed the object the script was attached to and didn’t remove it from the original object.