Home > Software design >  Synchronize "Score" with a timer? (Unity)
Synchronize "Score" with a timer? (Unity)

Time:03-10

I have a "Player score: 100 pts" text with a timer that counts from 50 seconds to 0. I want to make it so every 5 seconds that goes by, 10 points are removed from the "Player score". So every 5 seconds it would be 10, 90, 80, etc.. how can I achieve that? I'm using Unity and coding in C#

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

public class ScoreCountdownn : MonoBehaviour
{
    float scoreReductionTime = 5.0f;
    int scoreReductionAmount = 10;
    int maxScore = 100;
    int scoreReduction = 5;
    float timeUntilScoreReduction;
    int currentScore;

    void Start()
    {
        // Set the time until the next score reduction
        timeUntilScoreReduction = scoreReductionTime;
        // Set the current score to the max score
        currentScore = maxScore;
    }
    
    // Can be Update or FixedUpdate depending on your goal
    void Update()
    {
        // Reduce the time until score reduction 
        // by the time interval that has just passed
        timeUntilScoreReduction -= Time.deltaTime;
        
        // If the timeUntilScoreReduction reaches 0 or below, 
        // the score is reduced and the timeUntilScoreReduction 
        // is reset to the next time at which score reduction occurs
        if (timeUntilScoreReduction <= 0.0f)
        {
            currentScore -= scoreReduction;
            timeUntilScoreReduction = scoreReductionTime;
            ScoreCountdown.text = currentScore;
        }

        Debug.Log(timeUntilScoreReduction);
        Debug.Log(currentScore);
    }
}

CodePudding user response:

In your Update or FixedUpdate function, you can access Time.deltaTime. It is the difference in time in seconds since the last call of the Update or FixedUpdate function. Hence, you can keep track of how much time has passed, by adding up these time intervals between every repetition.

Since you didn't include any code, I'll try to help you with some general code that you can look at for reference.

EDIT: The code was altered when the original question was edited to provide more information.

using UnityEngine;
using UnityEngine.UI;

public class ExampleClass : MonoBehaviour
{
    float scoreReductionTime = 5.0f;
    int scoreReductionAmount = 10;
    int maxScore = 100;
    int scoreReduction = 5;
    float timeUntilScoreReduction;
    int currentScore;

    // You can drag your UI gameobject from the hierarchy 
    // to this field in the editor to link them.
    // That way Unity knows what the gameobject is without 
    // you having to specify it here in the code
    public GameObject ScoreCountdown;

    void Start()
    {
        // Set the time until the next score reduction
        timeUntilScoreReduction = scoreReductionTime;
        // Set the current score to the max score
        currentScore = maxScore;
        // The gameobject has components, one of which is the text component. 
        // You have to access this component and set its text value 
        // to the desired string. Since the currentScore is an integer, 
        // and text is a string, you have to make a string of the integer.
        ScoreCountdown.GetComponent<Text>().text = currentScore.ToString();
    }
    
    // Can be Update or FixedUpdate depending on your goal
    void Update()
    {
        // Reduce the time until score reduction 
        // by the time interval that has just passed
        timeUntilScoreReduction -= Time.deltaTime;
        
        // If the timeUntilScoreReduction reaches 0 or below, 
        // the score is reduced and the timeUntilScoreReduction 
        // is reset to the next time at which score reduction occurs
        if (timeUntilScoreReduction <= 0.0f)
        {
            currentScore -= scoreReduction;
            timeUntilScoreReduction = scoreReductionTime;
            ScoreCountdown.GetComponent<Text>().text = currentScore.ToString();
        }
    }
}

Note that in your question, you said that the score should decrease by 10 every 5 seconds, but then you gave the example of the score decreasing as 95, 90, 85, ... I chose to consider the first statement within the code, with a score reduction of 10 instead of the 5 in the second statement.

  • Related