使用单例模式时报错Object reference not set to an instance of an object
如果是在程序运行后,发现遗漏后再挂载的脚本,运行时任然出错,建议重启unity在进行尝试
//单例模式(playerInput为脚本名字)
public static playerInput instance;
private void Awake()
{
if (instance != null)
{
throw new System.Exception("存在多个单例");
}
else
{
Debug.Log("为空为空");
}
//为instance赋值(脚本挂载到player上,赋值后player)
instance = this;
}