0

I've a panel in my scene (ErrorMessage), I've been disabled it in the editor and writed this in my C# script:

            if(getUsernameResponse == "Login OK") {
                Application.LoadLevel("LobbyUI");
            } else {
                GameObject ErrorMessage = GameObject.FindGameObjectWithTag("ErrorMessage");
                ErrorMessage.SetActive(true);
            }

The script should enable (show) my ErrorMessage if getUsernameResponse have a different response of "Login OK".. but when I start the liveDemo I see this error:

NullReferenceException: Object reference not set to an instance of an object) in row:41 (ErrorMessage.SetActive(true);)

I've tried to enable the ErrorMessage from the editor and disable with

if(getUsernameResponse == "Login OK") {
                Application.LoadLevel("LobbyUI");
            } else {
                GameObject ErrorMessage = GameObject.FindGameObjectWithTag("ErrorMessage");
                ErrorMessage.SetActive(false);
            }

in my source and it works fine, how can I disable ErrorMessage (UI.Panel) from my script?

Thanks for support.

Mirko Brombin
  • 929
  • 3
  • 9
  • 32

2 Answers2

3

It simply means that:

GameObject ErrorMessage = GameObject.FindGameObjectWithTag("ErrorMessage");

is not finding the game object.

Probably because you did not actually put a tag on the GameObject, or the tag is spelled wrong. And be sure to remember layers are not tags!

Really though, I don't know your whole setup, but my suspicion is whatever your doing, you really shouldn't be doing. Creating a tag for an errorMessage dialogue? I've written a lot of UI's in Unity. Never have a I tagged anything in a UI. Tagging should be used for very generic grouping of types of objects in the scene that you need to easily grab as a group. TeamA, TeamB, AI, powerup. It should not be used for grabbing just one object, of a very specific nature.

I would use GameObject.Find and search for it by name of the actual GameObject.

Or I would do what Miron Alex said and create a slot in the inspector, then drag the GameObject into it. Which ideally should be a serialized private variable.

[SerializeField]
private GameObject errorMessage;
rygo6
  • 1,711
  • 18
  • 28
1

A NullReferenceException is thrown when an object is "null" as in, it does not exist. In your code, the method

GameObject.FindGameObjectWithTag("ErrorMessage"); 

didn't find any object with the "ErrorMessage" tag, which means it returned "null" and assigned "null" to the ErrorMessage GameObject. When you are trying to call a method on a "null" object it will throw a "NullReferenceException" because a "null" value doesn't know anything about the "SetActive(bool value)" method (As a GameObject does).

Make sure you have an object tagged as "errorMessage" in the scene. To make this easier, make a public GameObject in your code, name it ErrorMessage and assign it in the inspector.

public GameObject errorMessage;

if(getUsernameResponse == "Login OK") 
{
   Application.LoadLevel("LobbyUI");
} 
else 
{
   errorMessage.SetActive(false);
}

Should do the trick.

Miron Alex
  • 246
  • 1
  • 9
  • That does not really answer the question **when there is no object in the scene tagged as `ErrorMessage`** or assigned to your `errorMessage` field. Your code is **not** checking for `null`s. Plus the OP is using `tags`, not wanting to specifically assign an object to a behaviour. OP asked for fixes for _"Object reference not set to an instance of an object"_ which can still happen in your code. Consider revising. Good luck – MickyD Mar 02 '15 at 07:37
  • Thanks, i try this today, let you know. – Mirko Brombin Mar 02 '15 at 07:41
  • 1
    This not solved my problem, i've already declared ErrorMesage (ErrorMessage not errorMessage) in my script: GameObject ErrorMessage = GameObject.FindGameObjectWithTag("ErrorMessage"); – Mirko Brombin Mar 02 '15 at 07:46
  • You haven't answered if you have placed the tag "ErrorMessage" in the editor, which is likely the solution to your problem. – Diego Alares Mar 03 '15 at 07:55
  • this solved my problem thanks, I've declared my UI.Panel as a GameObject thanks. – Mirko Brombin Mar 03 '15 at 10:33