Add second in Timer

Hello, I have a Timer script for my game. In this game, If player kill the enemy, I wan to add 5 seconds to timer.
I have a score variable for enemey healt screen and working. (If kill the enemy change value)
And I want to use this variable and If score value change it, I want to add 5 second in timer.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ScoreManager : MonoBehaviour {

    public static int score;        // The player's score.
    Text text;                      // Reference to the Text component.
    public float startTime;
    private string seconds;
    private float addSecond = 5f;
    void Awake ()
    {
        // Set up the reference.
        text = GetComponent <Text> ();
        // Reset the score.
        score = 0;


        startTime = Time.time;


    }


    void Update ()
    {
        float t = Time.time - startTime ;
        string minutes = ((int)t / 60).ToString ();
        seconds = (t % 60).ToString ("f2");

        text.text = minutes + ":" + seconds;
    }
}

I do not know how can ı write code in update function ? In update function, I want to write if score value change it, Add 5 seconds to timer and then reset the score value = 0.

In enemy script call score value and change it.

Well, perhaps you want a “bonusTime” variable. You could then have this line:

float t = Time.time - startTime + bonusTime;

And maybe in the method that kills the enemy/increases your score, you add to that.

Thank u for answer I change the code I add “bonustTime” variable. But in my opinion I have two logical error;

  1. bonusTime value is 5 not mean 5 second. (Because seconds value = (t % 60) maybe bonusTime = 65)
    2)In my opinion I change time value but only one per frame because all code inside the update function.
public class ScoreManager : MonoBehaviour {

    public static int score;        // The player's score.
    Text text;                      // Reference to the Text component.
    public float startTime;
    private float bonusTime = 5f;
    private float t;
    void Awake ()
    {
        // Set up the reference.
        text = GetComponent <Text> ();
        // Reset the score.
        score = 0;


        startTime = Time.time;


    }


    void Update ()
    {
        // Set the displayed text to be the word "Score" followed by the score value.
        //text.text = "Score: " + score;

        if (score != 0) {
            t = Time.time - startTime + bonusTime;
            score = 0; // reset value for
                print("Add Time");
        }
        else
         t = Time.time - startTime;
       
        string minutes = ((int)t / 60).ToString ();
        string seconds = (t % 60).ToString ("f2");

        text.text = minutes + ":" + seconds;
    }
}

I am not sure what you’re doing with setting the score to zero.

Here’s a test script I wrote up based on mostly your code, with a simulation for gaining a score/bonus time from killing an enemy.

public class Test1 : MonoBehaviour {

    public static int score;        // The player's score.
    Text text;                      // Reference to the Text component.
    public float startTime;
    private float bonusTime = 5f;
    void Awake()
    {
        text = GetComponent<Text>();
        score = 0;
        startTime = Time.time;
    }

    void Update()
    {
        // simulate player killing enemy effect for score/bonus time increase.
        if (Input.GetMouseButtonDown(0))
        {
            AddScore();
        }
        float t = Time.time - startTime + bonusTime;

        string minutes = ((int)t / 60).ToString();
        string seconds = (t % 60).ToString("f2").PadLeft(5,'0');

        text.text = minutes + ":" + seconds;
    }
    void AddScore()
    {
        score++;
        bonusTime += 5;
    }
}

It’s working – at least so far in as I understood your question. :slight_smile:

Let me know if I got something mixed up.

1 Like

Thank u for this script I understand my fault.