请选择 进入手机版 | 继续访问电脑版
  • 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

c# - Unity: Null while making new class instance

[复制链接]
菜鸟教程小白 发表于 2022-4-23 21:05:46 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

1st debug gives me

    null
    UnityEngine.Debugog(Object)

at the same time setting up the parameters works, and 2nd debug gives me

   CaughtEnough
   UnityEngine.Debugog(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

   Invoke(rule, 0f);

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

   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

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


Best Answer-推荐答案


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

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap