Level counter gets stuck on the first phrase.

As you can tell by the title, I am making a level counter for a game. However, It always gets stuck on the first phrase which is “Tutorial”. After that it is supposed to advance to “1” and then up from there. My code looks like this.
On door to next level:

static var statLevel : int;

function OnTriggerEnter (){

	if (statLevel == null)
	{
		statLevel = 1;
	}

	statLevel ++;

	if (statLevel == 1)
	{
		Application.LoadLevel ("Challenge");
		MainScript.level = "Tutorial";
	}

	if (statLevel == 2)
	{
		Application.LoadLevel ("MageChallenge1");
		MainScript.level = "1";
	}

	if (statLevel == 3){
		Application.LoadLevel ("MageChallenge2");
		MainScript.level = "2";
	}

	if (statLevel == 4){
		Application.LoadLevel ("MageChallenge3");
		MainScript.level = "3";
	}

	if (statLevel == 5){
		Application.LoadLevel ("MageChallenge4");
		MainScript.level = "4";
	}

	if (statLevel == 6){
		Application.LoadLevel ("MageChallenge5");
		MainScript.level = "5";
	}

	if (statLevel == 7){
		Application.LoadLevel ("MageChallenge6");
		MainScript.level = "6";
	}

	if (statLevel == 8){
		Application.LoadLevel ("MageChallenge7");
		MainScript.level = "7";
	}

	if (statLevel == 9){
		Application.LoadLevel ("FinalTest");
	}
}

And in my main script:

static var deathLevel = "Challenge";
static var level : String;
public var deathMechanic : GameObject;
static var die = true;
static var isDying = false;

static function deathActivate (){
	if (die == true)
	{
		if (deathLevel == "Challenge")
		{
			NextLevel.statLevel = 0;
			level = "Tutorial";
		}

		if (deathLevel == "MageChallengeChallenge1")
		{
			NextLevel.statLevel = 1;
			level = "1";
		}

		if (deathLevel == "MageChallengeChallenge2")
		{
			NextLevel.statLevel = 2;
			level = "2";
		}

		if (deathLevel == "MageChallengeChallenge3")
		{
			NextLevel.statLevel = 3;
			level = "3";
		}

		if (deathLevel == "MageChallengeChallenge4")
		{
			NextLevel.statLevel = 4;
			level = "4";
		}

		if (deathLevel == "MageChallengeChallenge5")
		{
			NextLevel.statLevel = 5;
			level = "5";
		}

		if (deathLevel == "MageChallengeChallenge6")
		{
			NextLevel.statLevel = 6;
			level = "6";
		}

		if (deathLevel == "MageChallengeChallenge7")
		{
			NextLevel.statLevel = 7;
			level = "7";
		}

		die = false;
		Instantiate(activeDeathMechanic, player.position, Quaternion.identity);

		yield WaitForSeconds (5);

		Application.LoadLevel (deathLevel);
	}
}

function Start ()
{
	if (level == null)
	{
		level = "Tutorial";
	}

	die = true;
	if (NextLevel.statLevel == 1)
	{
		level = "Tutorial";
	}
	if (NextLevel.statLevel == 2)
	{
		level = "1";
	}
	if (NextLevel.statLevel == 3)
	{
		level = "2";
	}
	if (NextLevel.statLevel == 4)
	{
		level = "3";
	}
	if (NextLevel.statLevel == 5)
	{
		level = "4";
	}
	if (NextLevel.statLevel == 6)
	{
		level = "5";
	}
	if (NextLevel.statLevel == 7)
	{
		level = "6";
	}
	if (NextLevel.statLevel == 8)
	{
		level = "7";
	}
	if (NextLevel.statLevel == 9)
	{
		level = "Final";
	}
}

function Update (){
 	 if (NextLevel.statLevel == 2){
	deathLevel = "MageChallenge1";
}

if (NextLevel.statLevel == 3){
	deathLevel = "MageChallenge2";
}

if (NextLevel.statLevel == 4){
	deathLevel = "MageChallenge3";
}

if (NextLevel.statLevel == 5){
	deathLevel = "MageChallenge4";
}

if (NextLevel.statLevel == 6){
	deathLevel = "MageChallenge5";
}

if (NextLevel.statLevel == 7){
	deathLevel = "MageChallenge6";
}

if (NextLevel.statLevel == 8){
	deathLevel = "MageChallenge7";
}

}
Can anyone give me a logical reason as to why this doesn’t work and a way to fix it? Your help will be greatly appreciated.

Hi,

int is a value-type, so its default value is 0, but you are checking the field statLevel of type int for null in the first if-clause of the first script. Null is the default value of reference types. Iam not sure if this also applies to js but in c# it works that way.

I think some of this code can be tightened up a bit logically. It might not sort your problem but the code is currently open to some logical problems, for instance change:

if (statLevel == null)
     {
         statLevel = 1;
     }

to

if (statLevel <= 0)
     {
         statLevel = 1;
     }

Also change your long if statements to else ifs as if OnTriggerEnter is triggered within a short space of time, there is the possibility that statLevel will be incremented before the end of the function.

Have you used breakpoints or Debug.Log(statLevel) throughout the functions to see where it’s failing?