Search Unity

how to reset my timer while swithcing from one scene to other scene

Discussion in 'Scripting' started by UNITY3D_TEAM, Aug 28, 2012.

  1. UNITY3D_TEAM

    UNITY3D_TEAM

    Joined:
    Apr 23, 2012
    Posts:
    720
    how to reset my timer while switching from one scene to other scene.
    This is my timer script in scene 0:
    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4.     var startTime:float;
    5.      
    6.     var timeR:float;
    7.      
    8.   function Awake()
    9.      
    10.     {
    11.  
    12.     startTime=10.0;
    13.      
    14.     guiText.material.color=Color.blue;
    15.      
    16.     InvokeRepeating ("Countdown",0.0f,1.0f);
    17.      
    18.     }
    19.      
    20.      
    21.      
    22.     function Countdown(){
    23.      
    24.     timeR =startTime - Time.time;
    25.      
    26.     ShowTime();
    27.      
    28.     if(timeR<0)
    29.      
    30.     {
    31.      
    32.    
    33.  
    34. CancelInvoke();
    35.  
    36.  
    37.  
    38. TimeIsUp();
    39.      
    40.      
    41.     Debug.Log("TIME IS UP=" +timeR);
    42.      
    43.     }}
    44.      
    45.     function ShowTime()
    46.      
    47.     {
    48.      
    49.     var minutes:int;
    50.      
    51.     var seconds:int;
    52.    
    53.    
    54.      
    55.     var timeString:String;
    56.      
    57.     minutes=timeR / 60;
    58.    
    59.     seconds=timeR % 60;
    60.    
    61.    
    62.  
    63.    
    64.    timeString=minutes.ToString()+":"+seconds.ToString ("00");
    65.    // timeString=minutes.ToString()+":"+seconds.ToString("00");
    66.      
    67.     guiText.text=timeString;
    68.      
    69.     }
    70.      
    71.     function TimeIsUp()
    72.      
    73.     {
    74.      
    75.      
    76.      
    77.    guiText.text="Game Over";
    78.  
    79.     Application.LoadLevel(1);
    80.      
    81.     }
    82.  
    83.  
    84.  
    85.  
    86.  
    And this is my button script in scene1:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5.  
    6. public class trys : MonoBehaviour {
    7.  
    8.      
    9.     void Awake ()
    10.     {
    11.    
    12.        
    13.        
    14.     }
    15.    
    16.      
    17.     void Update ()
    18.     {
    19.    
    20.     }
    21.     void OnGUI()
    22.     {
    23.      
    24.     if(GUILayout.Button("TRY AGAIN"))
    25.            
    26.             {
    27.                
    28.             Application.LoadLevel(0);
    29.            
    30.             }
    31.            
    32.         }
    33.        
    34.     }
    35.    
    36.    
    37.  
    38.  
    39.  
    40.  
    41.  
     
  2. Loius

    Loius

    Joined:
    Aug 16, 2012
    Posts:
    546
  3. UNITY3D_TEAM

    UNITY3D_TEAM

    Joined:
    Apr 23, 2012
    Posts:
    720
    please help me i dont know where to use that in my script
     
  4. schetty

    schetty

    Joined:
    Jul 23, 2012
    Posts:
    424
    void OnLevelWasLoaded (){

    your function();
    }
     
  5. parandham03

    parandham03

    Joined:
    May 2, 2012
    Posts:
    174
    can u explain a little bit more......
     
  6. UNITY3D_TEAM

    UNITY3D_TEAM

    Joined:
    Apr 23, 2012
    Posts:
    720
    actually i having 2 scene in my script



    scene:0
    and
    scene:1


    i have timer script in scene 0 it runs 10seconds and after 10seconds .after 10seconds it will go to scene :1 .the scene1 contain button script it allow to go back to scene:0.but the problem is timer not reloading again when im gone back to scene 0 .this is problem "timer not starting from 10seconds again".it comming to game over and exiting to scene 1 .
     
    Last edited: Aug 28, 2012
  7. parandham03

    parandham03

    Joined:
    May 2, 2012
    Posts:
    174
    replace below line
    Code (csharp):
    1.  timeR =startTime - Time.time;
    by
    Code (csharp):
    1.  timeR =startTime-Time.timeSinceLevelLoad;