Unity3D 中的 NullReferenceException

NullReferenceException in Unity3D

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

Register.cs (class):

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

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

UIManagerLoginRegister.cs (class):

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

errorText 是一个public 文本,上面已经声明了,我是通过编辑器拖进去设置的。和errorCanvas一样,声明在函数上面,我是通过编辑器拖进去设置的

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

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
UnityEngine.MonoBehaviour:.ctor()
UIManagerLoginRegister:.ctor()
Register:attemptRegister() (at Assets/_scripts/Register.cs:81)
UnityEngine.EventSystems.EventSystem:Update()

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

我的第二个错误是:

NullReferenceException: Object reference not set to an instance of an object UIManagerLoginRegister.displayErrorMessage (System.String message) (at Assets/_scripts/UIManagerLoginRegister.cs:36)
Register.attemptRegister () (at Assets/_scripts/Register.cs:82)
UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:153)
UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:630)
UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:765)
UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:53)
UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44)
UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52)
UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269) UnityEngine.EventSystems.EventSystem:Update()

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

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

如果您从 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>();
}

尝试按照您的错误消息建议进行操作。

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

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

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

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

因此您需要创建 class:

的新实例
UIManagerLoginRegister uimlr = null;

完成后,您可以在 void Start 函数中设置它等于什么:

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

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