【问题标题】:NullReferenceException in Unity3D [duplicate]Unity3D中的NullReferenceException [重复]
【发布时间】:2016-06-27 14:28:36
【问题描述】:

我正在尝试编写游戏代码,并且在注册系统中,如果返回代码不是 2,则显示一个带有错误消息的弹出窗口。我制作了一个附有面板的新画布。附加在面板上的是一个按钮和一个文本。在我的代码中,如果服务器离线,我想调用这个函数:

Register.cs(类):

} catch(SocketException) {
    var uiclass = new UIManagerLoginRegister ();
    uiclass.displayErrorMessage ("Unable to connect to server. Please try again later.");
}

我必须使用该变量,以便可以在另一个名为 UIManagerLoginRegister.cs(类)的类中调用该函数。从那里该功能应该这样做:

UIManagerLoginRegister.cs(类):

public void displayErrorMessage(string message) {
    errorText.text = message;
    errorCanvas.enabled = true;
}

errorText 是一个公共文本,在上面声明,我通过编辑器将它拖入设置。和errorCanvas一样,是在函数上面声明的,我是通过编辑器拖进去设置的。

但我收到两个错误。第一个是:

您正在尝试使用“new”关键字创建 MonoBehaviour。这是不允许的。 MonoBehaviours 只能使用 AddComponent() 添加。或者,您的脚本可以继承自 ScriptableObject 或根本不继承基类
UnityEngine.MonoBehaviour:.ctor()
UIManagerLoginRegister:.ctor()
注册:attemptRegister()(在 Assets/_scripts/Register.cs:81)
UnityEngine.EventSystems.EventSystem:Update()

它不喜欢我设置 var 的方式,因此我可以使用其他类函数。那我该如何解决呢。

我的第二个错误是:

NullReferenceException:对象引用未设置为对象的实例 UIManagerLoginRegister.displayErrorMessage(System.String 消息)(在 Assets/_scripts/UIManagerLoginRegister.cs:36)
Register.attemptRegister () (在 Assets/_scripts/Register.cs:82)
UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (在 C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:153)
UnityEngine.Events.InvokableCallList.Invoke(System.Object[] 参数)(在 C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:630)
UnityEngine.Events.UnityEventBase.Invoke(System.Object[] 参数)(在 C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:765)
UnityEngine.Events.UnityEvent.Invoke () (在 C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:53)
UnityEngine.UI.Button.Press () (在 C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData)(在 C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44)
UnityEngine.EventSystems.ExecuteEvents.Execute(IPointerClickHandler 处理程序,UnityEngine.EventSystems.BaseEventData eventData)(位于 C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52)
UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler](UnityEngine.GameObject 目标,UnityEngine.EventSystems.BaseEventData eventData,UnityEngine.EventSystems.EventFunction`1 函子)(在 C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI /EventSystem/ExecuteEvents.cs:269) UnityEngine.EventSystems.EventSystem:Update()

我不明白为什么会出现第二个错误。请帮我修复这些错误。

【问题讨论】:

  • 第一个错误不会导致第二个错误吗?解决这个问题,我敢打赌它会消失。例如这样的:var uiclass = gameObject.AddComponent<UIManagerLoginRegister>();(PS 我不是 Unity 开发者,只是擅长 Google-fu)
  • NullReferenceException 是 .Net 中最没用的错误消息之一,当您在调试模式下查看代码并检查所有变量并找到为空的变量时,通常会在您调用时发生something.SomethingElsesomethingnull,因为 null 没有 SomethingElse 属性,您会收到错误

标签: c# object unity3d nullreferenceexception


【解决方案1】:

一旦你使用 MonoBehaviour,你就必须使用 GameObject.AddComponent

所以你会想要创建一个新的类实例:

UIManagerLoginRegister uimlr = null;

一旦你这样做了,你可以在 void Start 函数中设置它等于什么:

gameObject.AddComponent<UIManagerLoginRegister>();
uimlr = gameObject.GetComponent<UIManagerLoginRegister>();

从那里你应该没有更多的问题。

【讨论】:

    【解决方案2】:

    您正在尝试使用“new”关键字创建 MonoBehaviour。 这是不允许的。 MonoBehaviours 只能使用添加 添加组件()。或者,您的脚本可以继承自 ScriptableObject 或根本没有基类

    如果您从 MonoBehaviour 继承,您不能使用 new 关键字创建新实例。

    如果你有public class UIManagerLoginRegister {},你的代码就会工作。而不是public class UIManagerLoginRegister:MonoBehaviour {}

    MonoBehaviour 继承后,您应该使用GameObject.AddComponentInstantiate

    UIManagerLoginRegister loginRegister = null;
    void Start()
    {
       gameObject.AddComponent<UIManagerLoginRegister>();
       loginRegister = gameObject.GetComponent<UIManagerLoginRegister>();
    }
    

    如果UIManagerLoginRegister 附加到其他游戏对象(例如 UI 面板),您可以将其实例化为预制件。使其成为预制件,然后像下面这样实例化:

    public GameObject loginRegisterPrefab;
    UIManagerLoginRegister loginRegister;
    void Start()
    {
        GameObject tempObjct = Instantiate(loginRegisterPrefab) as GameObject;
        loginRegister = tempObjct.GetComponent<UIManagerLoginRegister>();
    }
    

    【讨论】:

      【解决方案3】:

      尝试遵循您的错误消息建议。

      您正在尝试使用“new”关键字创建 MonoBehaviour。这是不允许的。 MonoBehaviours 只能使用 AddComponent() 添加。或者,您的脚本可以继承自 ScriptableObject 或根本不继承基类

      您的UIManagerLoginRegister 可能继承自monobehaviour。如果您不要求它是 monobehaviour,因此不需要将其附加到游戏对象,请考虑将其删除。

      至于你的第二个错误。如果没有更多代码来验证它,我会假设没有分配 errorTexterrorCanvas。尝试在编辑器中分配它们,或通过start/awake

      【讨论】:

        猜你喜欢
        • 2017-03-22
        • 2015-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多