- Home /
Timer Continues After Object is disabled
So I'm making a game about tending to a campfire that goes out after 30 seconds after which you must relight it or its game over. So what I need my code to do is to continue the timer after the fire goes out until -15 seconds and then it's game over but the timer stops when the campfire is disabled. All my scripts are attached to the campfire. Here is the code.
using System.Collections; using System.Collections.Generic; using UnityEngine.SceneManagement; using UnityEngine;
public class Extinguish : MonoBehaviour { public float timeLeft = 30;
 // Start is called before the first frame update
 void Start()
 {
     
 }
 // Update is called once per frame
 void Update()
 {
     timeLeft -= Time.deltaTime;
     if (timeLeft <= 0.0f)                   
     {
         Debug.Log("Fire Out");
         gameObject.SetActive(false);
         //Destroy(gameObject);
     }
     if (!gameObject.activeSelf && timeLeft <= -15f)
     {
         Debug.Log("GameOver");
         SceneManager.LoadScene("GameOver");
     }
    
     
       
     
 }
   
}
Answer by NiGodfrin · Oct 15, 2020 at 09:21 PM
Hi, you can try to put your timer in a InvokeRepeating, it's not automatically stopped when disabling the GameObject. Or just move your script into an other GameObect who manage the CampFire.
Answer by Ady_M · Oct 16, 2020 at 01:30 AM
Create a separate "GameManager" object and let it handle the timer.
Your answer
 
 
             Follow this Question
Related Questions
timer not ticking down 2 Answers
Why does my MatchTime Class not work when using Properties? 1 Answer
CountDownTimer Help 1 Answer
Unity 5 - Adding a Pre-made Countdown Timer C# Script to the Game Scene as a Text UI Element 1 Answer
How do I make a Countdown Timer in Minutes and Seconds only 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                