[JS] Semicolon error?

Whenever I try to run my game, it gives me a semi-colon error even after I added a semicolon, I even tried taking it away. Why is it doing this?

#pragma strict

var LevelToLoad : int = 1;

var player : GameObject.FindGameObjectWithTag("Player");
function OnTriggerEnter (other : Collider){

        if (other.gameObject.tag == "LevelLoader")
            {
           
                Application.LoadLevel(LevelToLoad);
               
                var spawn : GameObject.FindGameObjectWithTag("Spawn");
               
                player.transform.position = spawn.transform.position;
            }
}

Your errors are on these lines:

var player : GameObject.FindGameObjectWithTag("Player");
var spawn : GameObject.FindGameObjectWithTag("Spawn");

The correct syntax is

var variable_name : variable_type;

Perform the Finds separately, and in the case of the first one, in a function such as Start.

So I fixed the errors, except now it doesn’t put the player at the spawn; Did I do something wrong?

#pragma strict

var LevelToLoad : int = 1;

var player : GameObject;

var spawn : GameObject;

function Start () {
    player = GameObject.FindGameObjectWithTag("Player");
}
function OnTriggerEnter (other : Collider){

        if(other.gameObject.tag == "LevelLoader")
            {
               
                Application.LoadLevel(LevelToLoad);
               
                spawn = GameObject.FindGameObjectWithTag("Spawn");
               
                player.transform.position = spawn.transform.position;
            }
}

The problem looks to be the

 Application.LoadLevel(LevelToLoad);

If you load a new level , all the resources including scripts are unloaded .

If you like to have the player be at the spawn position in the new level, just make a new script , attach it to a game object in the the scene that is newly loaded and write all the stuff like this:

    #pragma strict
    var player : GameObject;
    var spawn : GameObject;
    
    function Start () {
        player = GameObject.FindGameObjectWithTag("Player");
        spawn = GameObject.FindGameObjectWithTag("Spawn");
        player.transform.position = spawn.transform.position;
                }
    }
1 Like

Thanks, it works. :smile: