Why does if(levelToLoad ==null) not work?

I’m creating a script, here is a snippet,

var levelToLoad : String;

function Start () 
{
	if(levelToLoad ==null)
	{
		Debug.LogError("Have You Forgotten To Fill In levelToLoad?");
	}
}

So i want the editor to tell me that i forgot to fill it in if its empty, but this does not work… There is no Debug.LogError even if its empty… Thanks for any information.

strings aren’t supposed to be null–well atleast not in c#. if c# you would do:

if(String.IsNullOrEmpty(levelToLoad))

or

if(levelToLoad == “”)

one of those will work.

As opposed to the other answer you got, string can be null and should be null in C# and other language

It is a safe way to avoid crash while trying to use an non-initialized string.

// Crash
string str;
Console.WriteLine(str);
// No Crash
string str = null;
Console.WriteLine(str);

But a simple declaration does not set the string to null. You need to do it:

var levelToLoad : String = null;
 
function Start ()
{
  if(levelToLoad ==null)
  {
    Debug.LogError("Have You Forgotten To Fill In levelToLoad?");
  }
}

EDIT: I realized Unity does not take comparison (at least not in UnityScript) so you need to use:

if(String.IsNullOrEmpty(levelToLoad))