Unity:制作新的类实例时为Null [英] Unity: Null while making new class instance
问题描述
我陷入了愚蠢的局面:我正在创建泛型类的新实例,但它返回奇怪"的null.
I got stuck in pretty dumb situation: I'm making new instance of the generic class but it returns "weird" 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)
同时设置参数有效,第二次调试给了我
at the same time setting up the parameters works, and 2nd debug gives me
CaughtEnough
UnityEngine.Debug:Log(Object)
这应该是正确的类实例中的内容.
which is what supposed to be in the proper class instance.
给我带来的一个问题(到目前为止)是,如果我打电话给这个Rule类实例
One (only so far) issue that it is bringing to me is that if whitin this Rule class instance I call
Invoke(rule, 0f);
它给了我NullReferenceException错误.但同时实际功能
it gives me the NullReferenceException error. But at the same time the actual function
CaughtEnough();
工作正常且符合预期.
任何想法都可能是问题的根源,以及如何解决该问题?
Any ideas what could be the source of the problem and how to overcome it?
UPD也会按要求发布Rule类的设置部分,尽管它很简单
UPD also posting setup part of Rule class, as asked, though it is straightforward
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;
}
.....
推荐答案
public class Rule : MonoBehaviour{}
Rule rule2 = new Rule();
如果您要从MonoBehaviour
继承,则不能使用new
关键字创建新实例.
You can't use new
keyword to create new instance if you are inheriting from MonoBehaviour
.
您应该获得表示以下内容的异常:
您正在尝试使用'new'关键字创建MonoBehaviour. 这是不允许的. MonoBehaviours只能使用 AddComponent().另外,您的脚本可以继承 ScriptableObject或根本没有基类
You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all
如果您拥有public class Rule {}
,但您拥有public class Rule : MonoBehaviour {}
,那么您的代码就可以使用.
Your code would have worked if you had public class Rule {}
but you have public class Rule : MonoBehaviour {}
.
创建派生自MonoBehaviour
的类的新实例:
Creating new instance of class that derives from MonoBehaviour
:
示例类:
public class Rule : MonoBehaviour
{
public Rule(int i)
{
}
}
如果您是从MonoBehaviour
继承的,则应该使用GameObject.AddComponent
或Instantiate
为其创建新实例.
If you inherit from MonoBehaviour
, you should either use GameObject.AddComponent
or Instantiate
to create new instance of it.
Rule rule2 = null;
void Start()
{
rule2 = gameObject.AddComponent<Rule>();
}
OR
public Rule rulePrefab;
Rule rule2;
void Start()
{
rule2 = Instantiate(rulePrefab) as Rule;
}
如果Rule
脚本已经存在并已附加到GameObject,则无需创建/添加/实例化该脚本的新实例.只需使用GetComponent
函数从连接到的GameObject中获取脚本实例.
If the Rule
script already exist and is attached to the GameObject, you don't need to create/add/instantiate new instance of that script. Just use GetComponent
function to get the script instance from the GameObject it is attached to.
Rule rule2;
void Start()
{
rule2 = GameObject.Find("NameObjectScriptIsAttachedTo").GetComponent<Rule>();
}
您会注意到,从MonoBehaviour
派生脚本时,不能使用构造函数中的参数.
You will notice that you cannot use the parameter in the constructor when you derive your script from MonoBehaviour
.
创建不是源自MonoBehaviour
的类的新实例:
Creating new instance of class that does NOT derives from MonoBehaviour
:
示例类:(请注意,它不是衍生自"MonoBehaviour
"
public class Rule
{
public Rule(int i)
{
}
}
如果您不继承自MonoBehaviour
,则应使用new
关键字为其创建新实例.现在,如果需要,您可以使用构造函数中的参数.
If you don't inherit from MonoBehaviour
, you should use the new
keyword to create new instance of it. Now, you can use the parameter in the constructor if you want.
Rule rule2 = null;
void Start()
{
rule2 = new Rule(3);
}
编辑:
在最新版本的Unity中,使用new
关键字创建从MonoBehaviour
继承的脚本的新实例可能不会给您错误,也可能不是null
,但是所有回调函数都不会执行.这些功能包括Awake
,Start
,Update
功能和其他功能.因此,您仍然必须按照此答案顶部所述正确执行此操作.
In the latest version of Unity, creating new instance of a script that inherits from MonoBehaviour
with the new
keyword may not give you error and may not be null
too but all the callback functions will not execute. These includes the Awake
, Start
, Update
functions and others. So, you still have to do it properly as mentioned at the top of this answer.
这篇关于Unity:制作新的类实例时为Null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!