null text check should return null but apparently isn't null

Hello all,

As my question states I have a text check in an if statement to see if it is null but it comes back apparently not null when I clearly know there is no text in the text box.

Here is my code:

	GameObject LobbyWindow;

	void Start ()
		LobbyWindow = GameObject.FindGameObjectWithTag ("Lobby");
		LobbyWindow.SetActive (false);

public void StartServer()
		Network.InitializeServer(16, 7777, useNat);
		createLobby ();

void createLobby()
		LobbyWindow.SetActive (true);
		if (LobbyWindow.GetComponentInChildren<Text> ().text == null) 
			Text newtext = LobbyWindow.GetComponentInChildren<Text> ();
			newtext.text = this.userName.text;
			Debug.Log ("this is working");
			newtext = this.userName;

The LobbyWindow GameObject is a Panel and I have 8 empty text box’s in the Panel.

Any and all help is appreciated.

Kind Regards,


No text can mean that the string either empty (==“”) or null (==null). You can either check for both using

if (text == null || text == "")

or use the convenience method String.IsNullOrEmpty

if (String.IsNullOrEmpty(text))