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.