Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 调用外部变量时出现NullReferenceException_C#_Unity3d - Fatal编程技术网

C# 调用外部变量时出现NullReferenceException

C# 调用外部变量时出现NullReferenceException,c#,unity3d,C#,Unity3d,当我运行这段代码时,我在突出显示的行中得到一个NullReferenceException,即使我在另一个脚本中初始化了变量。引用脚本是一个简单的计时器,错误脚本正在尝试激活它 参考脚本: using System.Collections; using System.Collections.Generic; using UnityEngine; public class Timer : MonoBehaviour { public float timeRemaining = 10;

当我运行这段代码时,我在突出显示的行中得到一个NullReferenceException,即使我在另一个脚本中初始化了变量。引用脚本是一个简单的计时器,错误脚本正在尝试激活它

参考脚本:

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;
            }
        }
    }
}

错误脚本:

public void CMDelay(int delay)
{
    terminalInputText.enabled = false;
    Timer.timerIsRunning = true; //error here
    Timer.timeRemaining = delay;
    while (Timer.timerIsRunning == true){}
    terminalInputText.enabled = true;
}

@MuhammadAfzaal这是如何解决
NullReferenceException
<代码>计时器。timeRemaining不是静态的,没有实例无法访问。与所有其他
计时器。
字段相同,但该字段位于第一位。