Variables and how they reset after Application.LoadLevel

So after finally assembling a basic, working layout of my demo, I’ve found that once you click on the door at the end, running the Application.LoadLevel script and resetting the scene, a lot of variables aren’t as they should be. For example, the tram that is supposed to be stationary until you hop on it, immediately speeds off upon restart. Of course, after reading a few posts I discover, to my horror, that static variables do not reset upon scene loading. This points me to go through all of my apparent misuses of static (and there are quite a few of them), but it seems that some of the variables that aren’t resetting aren’t labeled as static.

var speed : float;
var stopAt : float;
var counter : float;
var go : boolean = false;

function OnTriggerEnter(trigger : Collider)
{
	if(trigger.gameObject.name == "Tram Collider")
	{
		go = true;
	}
}

function Update()
{
	var tram : GameObject = gameObject.Find("Tram");
	
	if(go == true)
	{
		tram.transform.Translate(0, 0, speed);
		counter += speed;
	}
	
	if(speed < 0)
	{
		if(counter < stopAt)
		{
			this.active = false;
		}
	}
	else
	{
		if(counter > stopAt)
		{
			this.active = false;
		}
	}
}

Here’s the piece of code I’ve spend the past ten minutes watching. It’s functionality is activated by the Collision-triggered variable, ‘go’. I have set many variables to static, but I have not set go, it seems. Is it actually the case that variables defined as var outside of the functions are automatically made static?

And while we’re asking questions, could someone please explain these variable states to me, in terms of restarting the level? As far as I can tell, there isn’t any other variable label which allows the access of one script from another; something central to my novice-code style because I cannot get .Find and .GetComponent to work for the life of me.

You should try initializing your variables when you first set them up to preventmost errors. Like the “var go : boolean = false;” always try to give the variable when you declare them a value, although it’s just 0 or something around that.

The static variables are available for all scripts, tey usually don’t need to be attached to an gameobject to work, also they only can store 1 value, but that should be clear :stuck_out_tongue:

I don’t know all behavious of the variables, but when you always reset them if you don’t need them, use statics variables or also variables within a gameobject that has “DontDestroyOnLoad” you’ll be fine. For example if you don’t setup a string and want to store a variable or something you get from a www request, you might end up with something like this “UndefinedActualValuesHere” and you can get errors, so it’s better to set the string to “” first.