Unity: Null while making new class instance

后端 未结 2 1240
Happy的楠姐
Happy的楠姐 2020-11-22 10:32

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 t         


        
相关标签:
2条回答
  • 2020-11-22 11:12

    Just a a follow up, how I ended up doing it and why:

    1. I no longer inherit the Rule class from MonoBehaviour to avoid tracking cretion and deletion of the gameObjects, which appeared to be the pain.

    2. As Invoke method does not exist in generic classes, I replaced it with reflection, as described here

    0 讨论(0)
  • 2020-11-22 11:23
    public class Rule : MonoBehaviour{}
    Rule rule2 = new Rule();
    

    You can't use new keyword to create new instance if you are inheriting from MonoBehaviour.

    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

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

    Creating new instance of class that derives from 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.

    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 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.



    Creating new instance of class that does NOT derives from MonoBehaviour:

    Example class: (Note that it does not derive from "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.

    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.

    0 讨论(0)
提交回复
热议问题