0

I am getting a NullReferenceException in the highlighted line when i run this code, even though i have initialised the variable in the other script. The reference script is a simple timer, and the error script is trying to activate it.

Reference Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Timer : MonoBehaviour
{
    public float timeRemaining = 10;
    public bool timerIsRunning = false;

    void Update()
    {
        if (timerIsRunning)
        {
            if (timeRemaining > 0)
            {
                timeRemaining -= Time.deltaTime;
            }
            else
            {
                timeRemaining = 0;
                timerIsRunning = false;
            }
        }
    }
}

Error Script:

public void CMDelay(int delay)
{
    terminalInputText.enabled = false;
    Timer.timerIsRunning = true; //error here
    Timer.timeRemaining = delay;
    while (Timer.timerIsRunning == true){}
    terminalInputText.enabled = true;
}
blue44
  • 1
  • @MuhammadAfzaal how is this solving a `NullReferenceException` ? .. – derHugo May 30 '21 at 12:33
  • `Timer.timeRemaining` is not static, and cannot be accessed without an instance. Same with all the other `Timer.` fields, but that one comes first. – NSJacob1 Jun 03 '21 at 14:29

0 Answers0