Unity C# - 尝试调用脚本中定义的 class 实例的方法
Unity C# - Trying to call a method of an instance of a class defined in a script
我正在尝试调用分配给 canvas 对象的函数,但我不知道应该使用什么方法:
ScoringSystem send;
send.AddPoints();
此方法无效,出现以下错误:
NullReferenceException: Object reference not set to an instance of an object
ObHost.Update () (at Assets/Scripts/ObHost.cs:39)
我从中调用函数的脚本名称是 ObHost
。
该函数位于名为 ScoringSystem
的脚本中。
该函数称为 AddPoints()
.
我真的不知道该怎么办,因为我尝试过其他方法,但它们涉及到游戏对象,而我的功能是在 canvas.
您没有初始化 class ScoringSystem
的实例 send
,因此引发 NullReferenceException
。
并且由于 send
是空引用,任何对成员或方法的调用都将失败并返回 NullReferenceException
。
为实例声明变量和初始化它是两件不同的事情,它们通常在一条语句中完成,调用构造函数 (new
) 或静态工厂方法。
你所要做的就是初始化变量 send
:
// Put any required or optional parameter in the constructor call
ScoringSystem send = new ScoringSystem();
我正在尝试调用分配给 canvas 对象的函数,但我不知道应该使用什么方法:
ScoringSystem send;
send.AddPoints();
此方法无效,出现以下错误:
NullReferenceException: Object reference not set to an instance of an object
ObHost.Update () (at Assets/Scripts/ObHost.cs:39)
我从中调用函数的脚本名称是 ObHost
。
该函数位于名为 ScoringSystem
的脚本中。
该函数称为 AddPoints()
.
我真的不知道该怎么办,因为我尝试过其他方法,但它们涉及到游戏对象,而我的功能是在 canvas.
您没有初始化 class ScoringSystem
的实例 send
,因此引发 NullReferenceException
。
并且由于 send
是空引用,任何对成员或方法的调用都将失败并返回 NullReferenceException
。
为实例声明变量和初始化它是两件不同的事情,它们通常在一条语句中完成,调用构造函数 (new
) 或静态工厂方法。
你所要做的就是初始化变量 send
:
// Put any required or optional parameter in the constructor call
ScoringSystem send = new ScoringSystem();