Unity:新实例时返回Null
Q:
I got stuck in pretty dumb situation: I’m making new instance of the generic class but it returns “weird” null.
我陷入了很愚蠢的情况:我正在制作通用类的新实例,但它返回“怪异” 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
1st debug gives me 第一次调试给我
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.
在适当的类实例中应该是什么。
One (only so far) issue that it is bringing to me is that if whitin this Rule class instance I call
它带给我的一个(唯一的)问题是,如果我称之为hitin这个规则类实例
Invoke(rule, 0f);
it gives me the NullReferenceException error. But at the same time the actual function
它给了我NullReferenceException错误。但同时实际功能
CaughtEnough();
works just fine and as expected.
正常工作正常。
Any ideas what could be the source of the problem and how to overcome it?
有什么想法是问题的根源以及如何克服问题?
UPD also posting setup part of Rule class, as asked, though it is straightforward
upd还按要求发布规则类的设置部分,尽管它很简单
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;
}
.....
A:
public class Rule : MonoBehaviour{}
Rule rule2 = new Rule();
You can’t use new
keyword to create new instance if you are inheriting from MonoBehaviour
.
如果您是从MonoBehaviour
继承的,则不能使用new
关键字创建新实例。
You should get exception that says:
您应该得到一个例外,说:
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
您正在尝试使用“新”关键字来创建一个单obehaviour。这是不允许的。只能使用AddComponent()添加MonoBehaviours。另外,您的脚本可以从ScriptableObject继承或根本没有基类
Your code would have worked if you had public class Rule {}
but you have public class Rule : MonoBehaviour {}
.
如果您有public class Rule {}
您的代码将有效 public class Rule : MonoBehaviour {}
。
Creating new instance of class that derives from MonoBehaviour
:
创建源自MonoBehaviour
的类的新实例:
Example class: 示例类:
public class Rule : MonoBehaviour
{
public Rule(int i)
{
}
}
If you inherit from MonoBehaviour
, you should either use GameObject.AddComponent
or Instantiate
to create new instance of it.
如果您从MonoBehaviour
继承,则应使用GameObject.AddComponent
或Instantiate
创建其新实例。
Rule rule2 = null;
void Start()
{
rule2 = gameObject.AddComponent<Rule>();
}
OR
public Rule rulePrefab;
Rule rule2;
void Start()
{
rule2 = Instantiate(rulePrefab) as Rule;
}
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
脚本已经存在并将其附加到GameObject,则无需创建/添加/实例化该脚本的新实例。只需使用GetComponent
函数即可从其附加的gameObject获取脚本实例。
Rule rule2;
void Start()
{
rule2 = GameObject.Find("NameObjectScriptIsAttachedTo").GetComponent<Rule>();
}
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
的类的新实例:
Example class: (Note that it does not derive from “MonoBehaviour
“
示例类:(请注意,它不会源自“ MonoBehaviour
”
public class Rule
{
public Rule(int i)
{
}
}
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.
如果您不从MonoBehaviour
继承,则应使用new
关键字来创建其新实例。现在,您可以在构造函数中使用参数。
Rule rule2 = null;
void Start()
{
rule2 = new Rule(3);
}
EDIT:
编辑:
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中,创建一个脚本的新实例,该脚本以new
关键字从MonoBehaviour
继承可能不会给您错误,并且可能不会null
,但所有回调函数都不会执行。这些包括Awake
, Start
, Update
功能和其他功能。因此,您仍然必须按照本答案的顶部进行正确的操作。
本站发布的内容若无意中侵犯到您的权益,请联系我们,本站将在一个工作日内删除。

912sy » Unity:新实例时返回Null