Unity: Null when creating a new instance of the class

I am stuck in a rather dumb situation: I am creating a new instance of a generic class, but it returns a "weird" zero.

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 debugging gives me

  null UnityEngine.Debug:Log(Object) 

at the same time, setting the parameters works, and the second debug file gives me

  CaughtEnough UnityEngine.Debug:Log(Object) 

which should be in the corresponding instance of the class.

One (only so far) problem that it brings to me is that if whitin this instance of the Rule class I call

  Invoke(rule, 0f); 

it gives me a NullReferenceException error. But at the same time, the actual function

  CaughtEnough(); 

working fine and as expected.

What ideas can be the source of the problem and how to overcome it?

UPD also sends part of the configuration of the Rule class, as specified, although it is simple.

 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; } ..... 
+5
source share
2 answers
 public class Rule : MonoBehaviour{} Rule rule2 = new Rule(); 

You cannot use the new keyword to create a new instance if you inherit from MonoBehaviour .

You should get an exception that says:

You are trying to create MonoBehaviour using the 'new' keyword. It is forbidden. MonoBehaviours can only be added using AddComponent (). Also, your script can inherit from ScriptableObject or no base class at all

Your code would work if you had a public class Rule {} , but you have a public class Rule : MonoBehaviour {} .

Create a new instance of the class derived from MonoBehaviour :

Class Example:

 public class Rule : MonoBehaviour { public Rule(int i) { } } 

If you inherit from MonoBehaviour , you must either use GameObject.AddComponent or Instantiate to create a new instance.

 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 exists and is attached to the GameObject, you do not need to create / add / create an instance of a new instance of this script. Just use the GetComponent function to get the script instance from the GameObject to which it is attached.

 Rule rule2; void Start() { rule2 = GameObject.Find("NameObjectScriptIsAttachedTo").GetComponent<Rule>(); } 

You will notice that you cannot use the parameter in the constructor when outputting the script from MonoBehaviour .



Creating a new instance of the class that is NOT derived from MonoBehaviour :

Class example: (Please note that it does not come from " MonoBehaviour "

 public class Rule { public Rule(int i) { } } 

If you do not inherit from MonoBehaviour , you should use the new keyword to create a new instance. Now you can use the parameter in the constructor if you want.

 Rule rule2 = null; void Start() { rule2 = new Rule(3); } 

EDIT

In the latest version of Unity, creating a new instance of the script that inherits from MonoBehaviour with the new keyword may not give you errors and may not be null either, but all callback functions will not be executed. They include the functions Awake , Start , Update and others. So you should still do it right, as indicated at the beginning of this answer.

+14
source

Just a sequel to how I did it and why:

  • I no longer inherit the Rule class from MonoBehaviour to avoid tracking the erasure and deletion of gameObjects that turned out to be a pain.

  • Since the Invoke method does not exist in the generic classes, I replaced it with reflection, as described here

+2
source

All Articles