C# 调用外部变量时出现NullReferenceException
当我运行这段代码时,我在突出显示的行中得到一个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;
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不是静态的,没有实例无法访问。与所有其他计时器。
字段相同,但该字段位于第一位。