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,
IHackedDeath.
Answer by maccabbe · Dec 14, 2015 at 03:06 AM
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))
https://msdn.microsoft.com/en-us/library/system.string.isnullorempty(v=vs.110).aspx
Um, ok.
I don't understand, why wouldn't it have both as the value so you could go either way but not have to check for both, that is very annoying and a nuisance.
Will give this a try and see how it goes, thank you maccabbe.
A string is a reference type and is allocated on the heap. A string, like any object, can be null. So there's actually no string object allocated, it's just a null reference. An actual string object can also represent an empty string. So there's still an object allocated and the variable holds a reference to that string object, but the string itself has zero length.
Empty strings are quite common and useful. For example:
string s = null;
Debug.Log(s.Length); // This will throw a null reference exception. You can't read the length of the string since the variable doesn't reference a string object.
s += "Hello World!"; // This also doesn't work since you can't concat null with a string.
With an empty string that's all possible:
string s = "";
Debug.Log(s.Length); // this will print "0"
s += "Hello";
s += " World!"; // now "s" contains "Hello World!".
Ins$$anonymous$$d of "" you can also use string.Empty
which is actually used by the compiler internally. See Empty for more details.
ps: This behaviour is nothing special in C#. It almost always works in a similar way in other languages.
Thank you very much! Your theoretical explanation clarified my doubts about the "error reference" I was having. I solved it by changing the declaration of the variable "string myvariable;" by "string myvariable =" "; Really thanks you
Your answer

Follow this Question
Related Questions
Use of if(Component) 1 Answer
if (Something != null) not good enough 2 Answers
How To Load TTF Font From External File 0 Answers
How to link up instantiated text in list to allow buttons to adjust number shown, c# 1 Answer
Adventure Game Help 0 Answers