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.