Search Unity

[JS] Semicolon error?

Discussion in 'Scripting' started by Krosskode, Sep 2, 2014.

  1. Krosskode

    Krosskode

    Joined:
    Mar 22, 2014
    Posts:
    4
    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?

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var LevelToLoad : int = 1;
    4.  
    5. var player : GameObject.FindGameObjectWithTag("Player");
    6. function OnTriggerEnter (other : Collider){
    7.  
    8.         if (other.gameObject.tag == "LevelLoader")
    9.             {
    10.            
    11.                 Application.LoadLevel(LevelToLoad);
    12.                
    13.                 var spawn : GameObject.FindGameObjectWithTag("Spawn");
    14.                
    15.                 player.transform.position = spawn.transform.position;
    16.             }
    17. }
     
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Your errors are on these lines:
    Code (csharp):
    1. var player : GameObject.FindGameObjectWithTag("Player");
    Code (csharp):
    1. var spawn : GameObject.FindGameObjectWithTag("Spawn");
    The correct syntax is
    Code (csharp):
    1. var variable_name : variable_type;
    Perform the Finds separately, and in the case of the first one, in a function such as Start.
     
  3. Krosskode

    Krosskode

    Joined:
    Mar 22, 2014
    Posts:
    4
    So I fixed the errors, except now it doesn't put the player at the spawn; Did I do something wrong?


    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var LevelToLoad : int = 1;
    4.  
    5. var player : GameObject;
    6.  
    7. var spawn : GameObject;
    8.  
    9. function Start () {
    10.     player = GameObject.FindGameObjectWithTag("Player");
    11. }
    12. function OnTriggerEnter (other : Collider){
    13.  
    14.         if(other.gameObject.tag == "LevelLoader")
    15.             {
    16.                
    17.                 Application.LoadLevel(LevelToLoad);
    18.                
    19.                 spawn = GameObject.FindGameObjectWithTag("Spawn");
    20.                
    21.                 player.transform.position = spawn.transform.position;
    22.             }
    23. }
     
  4. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    The problem looks to be the
    Code (CSharp):
    1.  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:

    Code (JavaScript):
    1.     #pragma strict
    2.     var player : GameObject;
    3.     var spawn : GameObject;
    4.    
    5.     function Start () {
    6.         player = GameObject.FindGameObjectWithTag("Player");
    7.         spawn = GameObject.FindGameObjectWithTag("Spawn");
    8.         player.transform.position = spawn.transform.position;
    9.                 }
    10.     }
    11.  
     
    Krosskode likes this.
  5. Krosskode

    Krosskode

    Joined:
    Mar 22, 2014
    Posts:
    4
    Thanks, it works. :D