Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by unity_185B120F33B4ADA87418 · Dec 12, 2021 at 06:23 PM · unity 2d

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 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

140 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges