Problem Restart Time by Key

Hi! My script work time but getkey “G” to reload time “Restart” is doesn’t work, You can check my script what’s wrong? Thanks!

private var startTime;
private var restartime;
private var restSeconds : int;
private var roundedRestSeconds : int;
private var displaySeconds : int;
private var displayMinutes : int;

var countDownSeconds : int;

function Awake() {
    startTime = Time.time * restartime;
}

function OnGUI () {
    var guiTime = Time.time - startTime;

    restSeconds = countDownSeconds - (guiTime);
    if (restSeconds == 60) {
        print ("One Minute Left");
    }
    if (restSeconds == 1790) {

    }

    roundedRestSeconds = Mathf.CeilToInt(restSeconds);
    displaySeconds = roundedRestSeconds % 60;
    displayMinutes = roundedRestSeconds / 60; 

    text = String.Format ("{0:00}:{1:00}", displayMinutes, displaySeconds); 
    GUI.Label (Rect (125,Screen.height - 75,135,20), text);
}

function Fixed() {
   if (Input.GetKeyDown("g")) {
   restartime = 1800;
   }
}

try in Update : function Update() { if (Input.GetKeyDown("g")) { restartime = 1800; Debug.Log("Resetting"); } }

3 Answers

3

Why don’t you just do it with something like this :

#pragma strict

var theTimer : float = 0.0;
var theStartTime : float = 120.0;

function Start() 
{
    theTimer = theStartTime;
}

function Update() 
{
    theTimer -= Time.deltaTime;

    if ( parseInt(theTimer) == 60) 
    {
        Debug.Log("One Minute Left");
    }

    if (theTimer <= 0) 
    {
        Debug.Log("OUT OF TIME");
        theTimer = 0;
    }

    if ( Input.GetKeyUp(KeyCode.G) )
    {
        theTimer = theStartTime;
    }
}

function OnGUI() 
{
    var text : String = String.Format ( "{0:00}:{1:00}", parseInt( theTimer / 60.0 ), parseInt( theTimer % 60.0 ) ); 

    GUI.Label( Rect( 125, Screen.height - 75, 135, 20), text );
}

Very thanks you!!

Again doesn’t work
private var startTime;
private var restartime;
private var restSeconds : int;
private var roundedRestSeconds : int;
private var displaySeconds : int;
private var displayMinutes : int;

var countDownSeconds : int;

function OnGUI () {
    var guiTime = Time.time - startTime;

 Debug.Log(startTime + "Resetting"+Time.time);
    restSeconds = countDownSeconds - (guiTime);
    if (restSeconds == 60) {
 ...
    }
    if (restSeconds == 1790) {
 ...
    }

    roundedRestSeconds = Mathf.CeilToInt(restSeconds);
    displaySeconds = roundedRestSeconds % 60;
    displayMinutes = roundedRestSeconds / 60; 

    text = String.Format ("{0:00}:{1:00}", displayMinutes, displaySeconds); 
    GUI.Label (Rect (125,Screen.height - 75,135,20), text);
}

function Update() { 
    if (Input.GetKeyDown("g")) { 
        restartime = 1800; 
        Debug.Log("Resetting");
    } 
}

Error: (Filename: Assets/RolePlay/Script RP/GUI/ProgressBar.js Line: 15) This Is wrong: function Awake() { startTime = Time.time * restartime; }

Have you given a value to restartTime? Put a Debug.Log(startTime + " "+Time.time); to see what is the problem.

doesn't work... :( You can copy my code check it error... and can't fix it. How can fix it?

Have you tried the answer I gave with my own way? It does work most definitely. I saw it running. Your pb is simple, nothing is happening. As simple as that. You reset restartime with the Input but you are not using it outside of the Awake. Obviously, that won't do much. It is startTime you need to reset. Using Time.time is wrong here, what if your game has been running for 30min, Time.time will be 30min. I gave you an answer that works independently of any time.

I found error: NullReferenceException: Object reference not set to an instance of an object

Please help!..