【发布时间】:2019-03-13 11:19:23
【问题描述】:
我陷入了非常愚蠢的境地:我正在创建泛型类的新实例,但它返回“奇怪”的 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)
这应该是正确的类实例中的内容。
它给我带来的一个(仅到目前为止)问题是,如果我调用这个 Rule 类实例
Invoke(rule, 0f);
它给了我 NullReferenceException 错误。但同时实际功能
CaughtEnough();
工作正常,符合预期。
任何想法可能是问题的根源以及如何克服它?
UPD 还按照要求发布了 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;
}
.....
【问题讨论】:
-
只需通过 if(rule2==null){Debug.Log("null")}else{Debug.Log("Not null")} 来检查 rule2 是否为空。还要发布您的 Rule 类,以便我们可以看到 RuleSetup 是什么。
-
谢谢,我按照你的建议检查了,rule2 确实为空。