我遇到了一个非常糟糕的情况:我正在创建一个泛型类的新实例,但它返回了“奇怪”的null值。
Rule rule2 = new Rule(); // initiate the class
Debug.Log(rule2); //1st debug
rule2.RuleSetup(r: "CaughtEnough", li: 0); //setting up the parameters
Debug.Log(rule2.rule); //2nd debug
第一个调试给了我
null
UnityEngine.Debug:Log(Object)
同时设置参数有效,并且第二次调试给了我结果。
CaughtEnough
UnityEngine.Debug:Log(Object)
这应该是在正确类实例中的内容。
目前唯一的问题是,如果在这个规则类实例中调用
Invoke(rule, 0f);
我的代码报了NullReferenceException异常,但实际上函数是可以正常运行的。
CaughtEnough();
这个功能正常运作并符合预期。
有什么想法可以解决问题的根源以及如何克服它吗?
根据要求,也发布了Rule类的设置部分,虽然很简单。
public class Rule : MonoBehaviour {
public string rule;
public int leftInt;
public Dictionary<string, int> leftDict;
public float countdown;
public int outcome;
public CatchManager catchMan;
public Net net;
// Use this for initialization
void Start () {
RuleSetup();
}
public void RuleSetup(string r = "NoRule", int li = 0, Dictionary<string, int> ld = null, float cd = float.PositiveInfinity) {
rule = r;
leftInt = li;
leftDict = ld;
countdown = cd;
}
.....