简体   繁体   中英

Subtracting Time From Timer In Unity Game C#

Recently, I've been working on a racing game that requires the player to avoid Radioactive Barrels that should subtract 15 seconds if they happen to collide into them; Below is code for my 'Timer' script, and my Barrel Collision Script.

Timer Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
public class Timer : MonoBehaviour
{
    public float timeRemaining = 10;
    public bool timerIsRunning = false;
    public Text timeText;
 
    public void Start()
    {
        // Starts the timer automatically
        timerIsRunning = true;
    }
 
    public void Update()
    {
        if (timerIsRunning)
        {
            if (timeRemaining > 0)
            {
                timeRemaining -= Time.deltaTime;
                DisplayTime(timeRemaining);
            }
            else
            {
                Debug.Log("Time has run out!");
                timeRemaining = 0;
                timerIsRunning = false;
            }
        }
    }
 
    public void DisplayTime(float timeToDisplay)
    {
        timeToDisplay += 1;
 
        float minutes = Mathf.FloorToInt(timeToDisplay / 60); 
        float seconds = Mathf.FloorToInt(timeToDisplay % 60);

        timeText.text = string.Format("{0:00}:{1:00}", minutes, seconds);
    }
}

Next, is my Barrel Collision Script:

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

public class ExplosionTrigger : MonoBehaviour 
{
  
    public AudioSource ExplosionSound;
    public ParticleSystem Explosion;
 
    public void OnTriggerEnter(Collider collider)
    {
        Explosion.Play();
        ExplosionSound.Play();
        Timer.timeRemaining += 1.0f;      
    }  
}

Is there anyway I could subtract time by colliding into said Barrels from the Timer script?

The easiest way to get the timeRemaining exposed to other classes is using the static keyword.

public static float timeRemaining = 10;

By making the variable static, it can be referenced by other classes. If you would rather not expose the variable completely, you can make static setter/getters. The variable would then be private static float timeRemaining = 10; when using the setter/getter.

public static float TimeRemaining
{
     get{ return timeRemaining;}
     set { timeRemaining = value;}
}

If you happen to want to expose more variables or methods to your classes from a script, I would recommend either implementing the Singleton Pattern or possibly implement your own Event System which uses the Singleton Pattern to pass events freely in your project. That way, you can subscribe and fire events for various scripts to listen for.

Yes, you could use GetComponent<>() to access the script from a different game object. You should set a GameObject variable, and drag the game object with the script on it. Add this to your barrel script:

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

public class ExplosionTrigger : MonoBehaviour 
{
  
    public AudioSource ExplosionSound;
    public ParticleSystem Explosion;
    public GameObject Timer;
    public float timeLoss;
    public Timer timer;

    void Start()
    {
        timer = Timer.GetComponent<TimerScript>();
    }
    public void OnTriggerEnter(Collider collider)
    {
        timer.timeRemaining -= 1f;
        Explosion.Play();
        ExplosionSound.Play();
    }  
}

You can change the values in the TimerScript using a period. Make sure to change TimerScript to the name of the script on the Timer game object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM