Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Application.LoadLevel Problems

Discussion in 'Scripting' started by caseyboundless, Jan 27, 2013.

  1. caseyboundless

    caseyboundless

    Joined:
    Oct 17, 2011
    Posts:
    590
    I can't seem to figure why my game is not transitioning properly from the menu, to lvl 1 , to lv1 2, if you die go back to the menu. When you get to lvl 2 it takes you straight back to the menu. It has to be the clock thats screwing it up?


    Here are my scripts and the webplayer. https://dl.dropbox.com/u/56545886/Pong1.2/Pong1.html



    This is the script in the menu scene.

    Code (csharp):
    1. //declare the raycast objects here so we dont need to instantiate them each frame
    2.  
    3. private var ray : Ray;
    4. private var RaycastHit : RaycastHit;
    5. var survivalScore3DText : TextMesh;
    6.  
    7. //*****************************************
    8.  
    9.  
    10.  function Awake()
    11. {
    12.   survivalScore3DText.text = "Survival Score: " + PlayerPrefs.GetInt("survivalScore").ToString();
    13.  
    14. }
    15.  
    16.  
    17.  
    18.  
    19.  
    20.  
    21.  
    22. function Update()
    23. {
    24.   if(Input.GetMouseButton(0))
    25.  {
    26.  
    27.    ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    28.    
    29.    if (Physics.Raycast (ray, RaycastHit))
    30.    {
    31.          transform.position.x = RaycastHit.point.x;
    32.    }
    33.  
    34.    if (RaycastHit.transform.name == "playButton")
    35.     {
    36.       audio.Play();
    37.       Application.LoadLevel("lvl1");
    38.     }
    39.  
    40.    }
    41.  
    42.  
    43. }









    This is the script on the ball.

    Code (csharp):
    1. var mainGameScript : MainGame;
    2.  
    3. var particles_splash : GameObject;
    4.  
    5.  
    6.  
    7. function Awake()
    8. {
    9.  
    10.    rigidbody.AddForce(5,5,0, ForceMode.Impulse);
    11.    InvokeRepeating("IncreaseBallVelocity", 2, 2);
    12. }
    13.  
    14. function Update()
    15. {
    16.  
    17.   //if(transform.position.y <-8)Application.LoadLevel("MenuPong");
    18.   if(transform.position.y <-8)
    19.    {
    20.        audio.Play();
    21.       mainGameScript.GameOver();
    22.     }
    23.  
    24. }





    This is in another script called MainGame.js where there is a function called Game Over()

    Code (csharp):
    1. function GameOver()
    2. {
    3.  
    4.   if(points > PlayerPrefs.GetInt("survivalScore"))          
    5.   {
    6.    PlayerPrefs.SetInt("survivalScore" , points);
    7.    audio.PlayOneShot(thirdSound);
    8.   }
    9.  
    10.    Application.LoadLevel("MenuPong");
    11.  
    12.  
    13. }




    This is the timer script for the last level. So when the clock ends the game should go back to the menu for right now.

    Code (csharp):
    1. var startTime : float;
    2. var timeRemaining : float;
    3.  
    4.  
    5.  
    6.  
    7.  
    8. function Start()
    9. {
    10.  startTime = 31.0;
    11.  guiText.material.color = Color.red;
    12. }
    13.  
    14. function Update()
    15. {
    16.  Countdown();
    17. }
    18.  
    19. function Countdown()
    20. {
    21.   timeRemaining = startTime - Time.time;
    22.   ShowTime();
    23.  
    24.   if(timeRemaining < 0)
    25.   {
    26.     timeRemaining = 0;
    27.     TimeIsUp();
    28.   }
    29. }
    30.  
    31. function ShowTime()
    32. {
    33.   var minutes: int;
    34.   var seconds: int;
    35.   var timeString: String;
    36.   minutes = timeRemaining /60;
    37.   seconds = timeRemaining%60;
    38.   timeString = minutes.ToString() + ":" + seconds.ToString("D2");
    39.   guiText.text = timeString;
    40.  
    41. }
    42.  
    43. function TimeIsUp()
    44. {
    45.  Debug.Log("Time is up");
    46.  Application.LoadLevel("MenuPong");
    47. }

    This script is the regular time script

    Code (csharp):
    1. var startTime : float;
    2. var timeRemaining : float;
    3.  
    4.  
    5.  
    6.  
    7.  
    8. function Start()
    9. {
    10.  startTime = 31.0;
    11.  guiText.material.color = Color.red;
    12. }
    13.  
    14. function Update()
    15. {
    16.  Countdown();
    17. }
    18.  
    19. function Countdown()
    20. {
    21.   timeRemaining = startTime - Time.time;
    22.   ShowTime();
    23.  
    24.   if(timeRemaining < 0)
    25.   {
    26.     timeRemaining = 0;
    27.     TimeIsUp();
    28.   }
    29. }
    30.  
    31. function ShowTime()
    32. {
    33.   var minutes: int;
    34.   var seconds: int;
    35.   var timeString: String;
    36.   minutes = timeRemaining /60;
    37.   seconds = timeRemaining%60;
    38.   timeString = minutes.ToString() + ":" + seconds.ToString("D2");
    39.   guiText.text = timeString;
    40.  
    41. }
    42.  
    43. function TimeIsUp()
    44. {
    45.  Debug.Log("Time is up");
    46.  Application.LoadLevel("lvl2");
    47.  Destroy(gameObject);
    48. }
     
    Last edited: Jan 27, 2013
  2. caseyboundless

    caseyboundless

    Joined:
    Oct 17, 2011
    Posts:
    590
    Instead of Application.LoadLevel(""); when the clock hits 0 in clock script i called a function called BackToMenu when it hits 0 from another script. No luck either.

    function BackToMenu()
    {

    Application.LoadLevel("");



    }
     
  3. freakdave

    freakdave

    Joined:
    Nov 18, 2012
    Posts:
    78
    You should just work with one timer script for all of your levels.
    Attach this timer script to all play levels (lvl1, lvl2, etc..) and see if it helps:

    Code (csharp):
    1.  
    2.     var startTime : float;
    3.     var timeRemaining : float;
    4.      
    5.     function Start()
    6.     {
    7.        startTime = 31.0;
    8.        guiText.material.color = Color.red;
    9.     }
    10.      
    11.     function Update()
    12.     {
    13.        Countdown();
    14.     }
    15.      
    16.     function Countdown()
    17.     {
    18.       timeRemaining = startTime - Time.timeSinceLevelLoad; //Time.time
    19.       ShowTime();
    20.      
    21.       if(timeRemaining < 0)
    22.       {
    23.         timeRemaining = 0;
    24.         TimeIsUp();
    25.       }
    26.     }
    27.      
    28.     function ShowTime()
    29.     {
    30.       var minutes: int;
    31.       var seconds: int;
    32.       var timeString: String;
    33.       minutes = timeRemaining /60;
    34.       seconds = timeRemaining%60;
    35.       timeString = minutes.ToString() + ":" + seconds.ToString("D2");
    36.       guiText.text = timeString;
    37.      
    38.     }
    39.      
    40.     function TimeIsUp()
    41. {
    42.     Debug.Log("Time is up");
    43.  
    44.     //switch or if else statements
    45.     if(Application.loadedLevelName == "lvl1") //first level
    46.     {
    47.         Application.LoadLevel("lvl2");
    48.         Destroy(gameObject); //i am not sure if you need this, when loading a new scene it gets auto-destroyed anyways
    49.     }
    50.     else
    51.     if(Application.loadedLevelName == "lvl2") //last level
    52.     {
    53.           Application.LoadLevel("MenuPong");
    54.     }
    55. }
    56.  
     
    Last edited: Jan 28, 2013