- Home /
I'm making a quiz game for a college project and have no experience with Unity. my codes from madfireon Is it possible to reset the timer after changing the question?
using System.Collections; using System.Collections.Generic; using UnityEngine; using System; using UnityEngine.UI; using UnityEngine.SceneManagement;
public class QuestionManager : MonoBehaviour { [SerializeField] private QuestionUi questionUi; [SerializeField]private List questions; [SerializeField] private float timeLimit = 30f; private Question selectedQuestion; private int scoreCount = 0; private int highscore = 0; private float currentTime; private GameStatus gameStatus = GameStatus.Next; public GameStatus GameStatus { get { return gameStatus; } }
public void Start()
{
scoreCount = 0;
currentTime = timeLimit;
SelectQuestion();
gameStatus = GameStatus.Playing;
}
void SelectQuestion()
{
int val = UnityEngine.Random.Range(0, questions.Count);
selectedQuestion = questions[val];
questionUi.SetQuestion(selectedQuestion);
questions.RemoveAt(val);
}
private void Update()
{
if (gameStatus == GameStatus.Playing)
{
highscore = PlayerPrefs.GetInt("highscore", 0);
currentTime -= Time.deltaTime;
SetTimer(currentTime);
}
}
private void SetTimer(float value)
{
TimeSpan time = TimeSpan.FromSeconds(currentTime);
questionUi.TimerText.text = time.ToString("mm':'ss");
if (currentTime < 0)
{
gameStatus = GameStatus.Next;
questionUi.GameOverPanel.SetActive(true);
}
}
public bool Answer(string answered)
{
bool correctAns = false;
if (answered == selectedQuestion.correctAns)
{
correctAns = true;
scoreCount += 1;
questionUi.ScoreText.text = "" + scoreCount;
questionUi.HighScoreText.text = highscore + "";
if (highscore < scoreCount)
{
PlayerPrefs.SetInt("highscore", scoreCount);
highscore = scoreCount;
}
}
else
{
}
if (gameStatus == GameStatus.Playing)
{
if (questions.Count > 0)
{
Invoke("SelectQuestion", 0.4f);
}
else
{
gameStatus = GameStatus.Next;
questionUi.GameOverPanel.SetActive(true);
}
}
return correctAns;
}
public void TryButton()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
[System.Serializable] public class Question { public string questionInfo; public QuestionType questionType; public List options; public string correctAns; }
[System.Serializable]
public enum QuestionType { TEXT } public enum GameStatus { Next, Playing }
Answer by Monsoonexe · May 04 at 12:55 AM
From the code provided, I'm not sure how your timer is implemented. I imagine you could do something like currentTime = timePerQuestion;
or something similar.
This code won't work if you copy-paste it, but if you like are looking for inspiration, this is the Timer code I've used in 5+ projects: https://github.com/Monsoonexe/Rich-Unity-Package/blob/master/_RichScripts/Timer.cs
Your answer
![](https://koobas.hobune.stream/wayback/20220613050500im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
MissingReferenceException: 0 Answers
Use Sprites as Score 1 Answer
Check whether any collision has occured to spawned game objects 0 Answers
Image as boundary 1 Answer
Unity 2d navmesh for a tilemap 1 Answer