Unity3D 中的 NullReferenceException [重复]

NullReferenceException in Unity3D [duplicate]

提问人:Madyson 提问时间:6/27/2016 最后编辑:FilburtMadyson 更新时间:6/28/2016 访问量:1824

问:

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

寄存器.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() Register: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[] 参数) (在 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()

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

C# 对象 unity-game-engine nullreferenceexception

评论

0赞 DavidG 6/27/2016
第一个错误不会导致第二个错误吗?解决这个问题,我敢打赌它会消失。例如:(PS 我不是 Unity 开发人员,只是擅长 Google-fu)var uiclass = gameObject.AddComponent<UIManagerLoginRegister>();
0赞 MikeT 6/27/2016
NullReferenceException 是 .Net 中最无用的错误消息之一,当您在调试模式下查看代码并检查所有变量并找到 null 时,它通常会发生在您调用 和 is 时,并且由于没有属性,因此您会收到错误something.SomethingElsesomethingnullnullSomethingElse

答:

1赞 Programmer 6/27/2016 #1

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

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

Your code would have worked if you had . instead of public class UIManagerLoginRegister {}public class UIManagerLoginRegister:MonoBehaviour {}

Once you inherit from , you should either use or .MonoBehaviourGameObject.AddComponentInstantiate

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

OR

You can instantiate it as a prefab if is attached to other GameObjects such as UI Panels. Make it a prefab then instantiate like below:UIManagerLoginRegister

public GameObject loginRegisterPrefab;
UIManagerLoginRegister loginRegister;
void Start()
{
    GameObject tempObjct = Instantiate(loginRegisterPrefab) as GameObject;
    loginRegister = tempObjct.GetComponent<UIManagerLoginRegister>();
}
0赞 MX D 6/27/2016 #2

Try following your errors messages advice.

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 probably inherits from a . If you do not require it to be a , so do not require it to be attached to a gameobject, consider removing this. UIManagerLoginRegistermonobehaviourmonobehaviour

As for your second error. Without more code to validate it, I would assume that either or is not assigned. Try assigning them in the editor, or through the errorTexterrorCanvasstart/awake

2赞 Lauthai 6/28/2016 #3

Once you use MonoBehaviour you have to use GameObject.AddComponent

So you will want to make a new instance of the class:

UIManagerLoginRegister uimlr = null;

Once you do that, you can set what it is equal to inside of the void Start function:

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

From there you should have no more issues.