- Home /
Question by
charleneconcha · Oct 12, 2020 at 07:35 AM ·
timerreload
MY TIMER RESTART WHEN RELOADING THE SCENE.
WHEN RELOAD MY SCENE BECAUSE OF WRONG INPUT I WANT MY TIMER IS STILL RUNNING IT WILL STOP IF THEY PUT THE CORRECT POSITION
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Security.Cryptography;
public class vRingLevel1GameControl1 : MonoBehaviour
{
public static vRingLevel1GameControl1 instance;
public GameObject vRingL1Close, vRingLevel1Close2, vRingLevel1Slide, vRingPole, vRingBlue1, vRingRed1, vRingYellow1;
public Text timeCounter, countdownText, congratsText;
public bool gamePlaying { get; private set; }
public int countdownTime;
private float startTime, elapsedTime;
TimeSpan timePlaying;
public static int slotsOccupied;
[SerializeField]
private Transform[] rings;
[SerializeField]
private GameObject wrongSign;
private void Awake()
{
instance = this;
}
private void Start()
{
timeCounter.gameObject.SetActive(false);
congratsText.gameObject.SetActive(false);
vRingL1Close.SetActive(false);
vRingLevel1Close2.SetActive(false);
vRingLevel1Slide.SetActive(false);
vRingPole.SetActive(false);
vRingBlue1.SetActive(false);
vRingRed1.SetActive(false);
vRingYellow1.SetActive(false);
timeCounter.text = "00:00";
gamePlaying = false;
StartCoroutine(CountdownToStart());
vRingLevel1Drag.PuzzleDone += CheckResults;
slotsOccupied = 0;
wrongSign.SetActive(false);
}
private void BeginGame()
{
gamePlaying = true;
startTime = Time.time;
}
private void Update()
{
if (vRingLevel1Close.locked)
{
vRingLevel1Close.locked = false;
SceneManager.LoadScene("vRingLevelUI");
}
if (gamePlaying)
{
elapsedTime = Time.time - startTime;
timePlaying = TimeSpan.FromSeconds(elapsedTime);
string timePlayingStr = timePlaying.ToString("mm':'ss");
timeCounter.text = timePlayingStr;
}
}
public void CheckResults()
{
if (rings[0].position.y == -0.04f &&
rings[1].position.y == -1.81f &&
rings[2].position.y == -3.62f)
{
gamePlaying = false;
congratsText.gameObject.SetActive(true);
}
else
{
wrongSign.SetActive(true);
Invoke("ReloadGame", 1f);
}
}
private void ReloadGame()
{
vRingLevel1Drag.PuzzleDone -= CheckResults;
SceneManager.LoadScene("vRingLevel1");
}
IEnumerator CountdownToStart()
{
while (countdownTime > 0)
{
countdownText.text = countdownTime.ToString();
yield return new WaitForSeconds(1f);
countdownTime--;
}
BeginGame();
countdownText.text = "GO!";
yield return new WaitForSeconds(1f);
timeCounter.gameObject.SetActive(true);
countdownText.gameObject.SetActive(false);
congratsText.gameObject.SetActive(false);
vRingL1Close.SetActive(true);
vRingBlue1.SetActive(true);
vRingRed1.SetActive(true);
vRingYellow1.SetActive(true);
vRingPole.SetActive(true);
}
}
Comment
Have you tried all of these solutions? https://answers.unity.com/questions/1219988/countdown-to-persist-through-scenes.html
i will try it hope this code will help me. i will update you. im a beginner in making a 2d unity game. thank you for your response
Your answer

Follow this Question
Related Questions
Reload level after timer hits 0 1 Answer
Time.deltaTime stops counting up, after the Player is destroyed 1 Answer
Reload Courutine Taking To Much Ammo 1 Answer
Time.time stops working after reloading a level 4 Answers
How do I Reset Scene 1 Answer