简体   繁体   中英

NullReferenceException in Unity3D

I am trying to code a game and in the registration system, if the return code is something other than a two, to show a popup with an error message on it. I made a new canvas with a panel attached. And attached to the panel is a button and a text. In my code, if the server is offline I want to call this function:

Register.cs (class):

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

I have to use that var so I can call that function in a different class called UIManagerLoginRegister.cs (class). From there the function is supposed to do this:

UIManagerLoginRegister.cs (class):

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

The errorText is a public Text, and is declared above, and I set it by dragging it in through the editor. Same with the errorCanvas, it is declared above the function, and I set it by dragging it in through the editor.

But I am receiving two errors. The first is:

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()

It doesn't like how I set my var so I can use the other classes function. So how would I fix that.

And my second error is:

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()

I don't understand why I am getting this second error. Please help me fix these errors.

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.

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 new keyword to create new instance if you are inheriting from MonoBehaviour .

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

Once you inherit from MonoBehaviour , you should either use GameObject.AddComponent or Instantiate .

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

OR

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM