Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 委托中的Unity arg NullReference_C#_Unity3d - Fatal编程技术网

C# 委托中的Unity arg NullReference

C# 委托中的Unity arg NullReference,c#,unity3d,C#,Unity3d,此代码由委托调用: private void OnMsgBoxClick(object arg) { if (arg.GetType() == typeof(int)) { this.LogWarning("OnMsgBoxClick() arg: {0}", arg.ToString()); } } 它给出了NullReferenceException,但之后仍然调试这句话,如下所示: NullReferenceException: Object re

此代码由委托调用:

private void OnMsgBoxClick(object arg)
{
    if (arg.GetType() == typeof(int))
    {
        this.LogWarning("OnMsgBoxClick() arg: {0}", arg.ToString());
    }
}
它给出了NullReferenceException,但之后仍然调试这句话,如下所示:

NullReferenceException: Object reference not set to an instance of an object
> 21:29:11.777 Example_MsgBox::Invoke() OnMsgBoxClick() arg: 0
我希望调试程序不会出错。我如何处理这个问题


这是我的错误调用堆栈

NullReferenceException: Object reference not set to an instance of an object
Example_MsgBox.OnMsgBoxClick (System.Object arg) (at Assets/GamePlay/Example/Example_MsgBox.cs:28)
GamePlay.UI.UIAPI+<ShowMsgBox>c__AnonStorey0.<>m__0 (System.Object closeArg) (at Assets/GamePlay/UI/UIAPI.cs:32)
SGF.UI.FrameWork.UIWindow.OnDisable () (at Assets/SGF/UI/FrameWork/UIWindow.cs:73)
UnityEngine.GameObject:SetActive(Boolean)
SGF.UI.FrameWork.UIWindow:Close(Object) (at Assets/SGF/UI/FrameWork/UIWindow.cs:119)
GamePlay.UI.Common.UIMsgBox:OnBtnClick(Int32) (at Assets/GamePlay/UI/Common/UIMsgBox.cs:62)
UnityEngine.EventSystems.EventSystem:Update()

这是“UIWindow”:

    public delegate void CloseEvent(object arg = null);
    public event CloseEvent OnCloseEvent;
    public sealed override void Close(object arg = null)
    {
        this.Log("Close() arg:{0}", arg);
        if (this.gameObject.activeSelf)
        {
            this.gameObject.SetActive(false);
        }
        OnClose(arg);
        if (OnCloseEvent != null)
        {
            OnCloseEvent(arg);
            OnCloseEvent = null;
        }
    }

对不起,我觉得太长了

谢谢

添加此支票

private void OnMsgBoxClick(object arg)
{
    if (arg != null && arg.GetType() == typeof(int))
    {
        this.LogWarning("OnMsgBoxClick() arg: {0}", arg.ToString());
    }
}
加上这张支票

private void OnMsgBoxClick(object arg)
{
    if (arg != null && arg.GetType() == typeof(int))
    {
        this.LogWarning("OnMsgBoxClick() arg: {0}", arg.ToString());
    }
}

是否有可能调用两次
OnMsgBoxClick
?不,我确信它只调用一次。我想可能是代理先运行,然后得到arg。但我不确定我的猜测。可能是@gunr217的副本。你读过这篇文章吗?是的,我读过这篇文章。你有可能调用
OnMsgBoxClick
两次吗?不,我肯定它只调用一次。我想可能是代理先运行,然后得到arg。但是我不确定我的猜测。可能是@gunr217的复制品。你读过这篇文章吗?是的,我读过这篇文章。谢谢。但是,如果我添加该检查,我的调试程序将不会出现。@Chan.Wunsam那么这意味着您的参数为null,并且您看到的没有该参数的调试语句是由于调用null对象上的方法而导致的未定义行为造成的。@Eddge谢谢。我添加了我的调用堆栈。谢谢。但是,如果我添加该检查,我的调试程序将不会出现。@Chan.Wunsam那么这意味着您的参数为null,并且您看到的没有该参数的调试语句是由于调用null对象上的方法而导致的未定义行为造成的。@Eddge谢谢。我添加调用堆栈。