0

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.

Filburt
  • 17,626
  • 12
  • 64
  • 115
Madyson
  • 33
  • 6
  • Doesn't the first error cause the second one? Fix that and I bet it goes away. For example something like this: `var uiclass = gameObject.AddComponent();` (PS I'm not a Unity dev, just good at Google-fu) – DavidG Jun 27 '16 at 14:37
  • NullReferenceException is one of the least useful error messages in .Net, when you see one look at your code in debug mode and examine all your variable and find the one that's null, it usually happens when you call `something.SomethingElse` and `something` is `null`, and because `null` doesn't have a `SomethingElse` property you get the error – MikeT Jun 27 '16 at 14:47

3 Answers3

2

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.

Lauthai
  • 305
  • 1
  • 12
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 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>();
}
Programmer
  • 121,791
  • 22
  • 236
  • 328
0

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

MX D
  • 2,453
  • 4
  • 35
  • 47