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.
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;