• 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
Question by boss2070301 · Apr 15, 2022 at 10:57 PM · selectionsequenceiterate

How to add sequencing selection and iteration

I was making a game for my class because we had to write a program and because I had experience with unity I decided to make a short little trivia game but now that i am done with the code and I am doing the written segment I just realized that I have to have The first program code segment must be a student-developed procedure that: Defines the procedure's name and return type (if necessary) Contains and uses one or more parameters that have an effect on the functionality of the procedure Implements an algorithm that includes sequencing, selection and iteration

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Timer : MonoBehaviour
 {
     [SerializeField] float timeToCompleteQuestion = 30f;
     [SerializeField] float timeToShowCorrectAnswer = 10f;
 
     public bool loadNextQuestion;
     public float fillFraction;
     public bool isAnsweringQuestion;
     
     float timerValue;
 
     void Update()
     {
         UpdateTimer();
     }
 
     public void CancelTimer()
     {
         timerValue = 0;
     }
 
     void UpdateTimer()
     {
         timerValue -= Time.deltaTime;
 
         if(isAnsweringQuestion)
         {
             if(timerValue > 0)
             {
                 fillFraction = timerValue / timeToCompleteQuestion;
             }
             else
             {
                 isAnsweringQuestion = false;
                 timerValue = timeToShowCorrectAnswer;
             }
         }
         else
         {
             if(timerValue > 0)
             {
                 fillFraction = timerValue / timeToShowCorrectAnswer;
             }
             else
             {
                 isAnsweringQuestion = true;
                 timerValue = timeToCompleteQuestion;
                 loadNextQuestion = true;
             }
         }
     }
 }
 
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ScoreKeeper : MonoBehaviour
 {
     int correctAnswers = 0;
     int questionsSeen = 0;
 
     public int GetCorrectAnswers()
     {
         return correctAnswers;
     }
 
     public void IncrementCorrectAnswers()
     {
         correctAnswers++;
     }
 
     public int GetQuestionSeen()
     {
         return questionsSeen;
     }
 
     public void IncrementQuestionsSeen()
     {
         questionsSeen++;
     }
 
     public int CalculateScore()
     {
         return Mathf.RoundToInt(correctAnswers / (float)questionsSeen * 100);
     }
 }
 
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using TMPro;
 using UnityEngine.UI;
 
 public class Quiz : MonoBehaviour
 {
     [Header("Questions")]
     [SerializeField] TextMeshProUGUI questionText;
     [SerializeField] List<QuestionSO> questions = new List<QuestionSO>();
     QuestionSO currentQuestion;
 
     [Header("Answers")]
     [SerializeField] GameObject[] answerButtons;
     int correctAnswerIndex;
     bool hasAnsweredEarly = true;
 
     [Header("Button Colors")]
     [SerializeField] Sprite defaultAnswerSprite;
     [SerializeField] Sprite correctAnswerSprite;
 
     [Header("Timer")]
     [SerializeField] Image timerImage;
     Timer timer;
 
     [Header("Scoring")]
     [SerializeField] TextMeshProUGUI scoreText;
     ScoreKeeper scoreKeeper;
 
     [Header("ProgressBar")]
     [SerializeField] Slider progressBar;
 
     public bool isComplete;
 
     void Awake()
     {
         timer = FindObjectOfType<Timer>();
         scoreKeeper = FindObjectOfType<ScoreKeeper>();
         progressBar.maxValue = questions.Count;
         progressBar.value = 0;
     }
 
     void Update()
     {
         timerImage.fillAmount = timer.fillFraction;
         if (timer.loadNextQuestion)
         {
             if (progressBar.value == progressBar.maxValue)
             {
                 isComplete = true;
                 return;
             }
 
             hasAnsweredEarly = false;
             GetNextQuestion();
             timer.loadNextQuestion = false;
         }
         else if (!hasAnsweredEarly && !timer.isAnsweringQuestion)
         {
             DisplayAnswer(-1);
             SetButtonState(false);
         }
     }
 
     public void OnAnswerSelected(int index)
     {
         hasAnsweredEarly = true;
         DisplayAnswer(index);
         SetButtonState(false);
         timer.CancelTimer();
         scoreText.text = "Score: " + scoreKeeper.CalculateScore() + "%";
     }
 
     void DisplayAnswer(int index)
     {
         Image buttonImage;
         if (index == currentQuestion.GetCorrectAnswerIndex())
         {
             questionText.text = "Correct!";
             buttonImage = answerButtons[index].GetComponent<Image>();
             buttonImage.sprite = correctAnswerSprite;
             scoreKeeper.IncrementCorrectAnswers();
         }
         else
         {
             correctAnswerIndex = currentQuestion.GetCorrectAnswerIndex();
             string correctAnswer = currentQuestion.GetAnswer(correctAnswerIndex);
             questionText.text = "Sorry, the correct answer was;\n" + correctAnswer;
             buttonImage = answerButtons[correctAnswerIndex].GetComponent<Image>();
             buttonImage.sprite = correctAnswerSprite;
         }
     }
 
     void GetNextQuestion()
     {
         if (questions.Count > 0)
         {
             SetButtonState(true);
             SetDefaultButtonSprites();
             GetRandomQuestion();
             DisplayQuestion();
             progressBar.value++;
             scoreKeeper.IncrementQuestionsSeen();
         }
     }
 
     void GetRandomQuestion()
     {
         int index = Random.Range(0, questions.Count);
         currentQuestion = questions[index];
 
         if (questions.Contains(currentQuestion))
         {
             questions.Remove(currentQuestion);
         }
     }
 
     void DisplayQuestion()
     {
         questionText.text = currentQuestion.GetQuestion();
 
         for (int i = 0; i < answerButtons.Length; i++)
         {
             TextMeshProUGUI buttonText = answerButtons[i].GetComponentInC$$anonymous$$ldren<TextMeshProUGUI>();
             buttonText.text = currentQuestion.GetAnswer(i);
         }
     }
 
     void SetButtonState(bool state)
     {
         for (int i = 0; i < answerButtons.Length; i++)
         {
             Button button = answerButtons[i].GetComponent<Button>();
             button.interactable = state;
         }
     }
 
     void SetDefaultButtonSprites()
     {
         for (int i = 0; i < answerButtons.Length; i++)
         {
             Image buttonImage = answerButtons[i].GetComponent<Image>();
             buttonImage.sprite = defaultAnswerSprite;
         }
     }
 }
 
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [CreateAssetMenu(menuName = "Quiz Question", fileName = "New Question")]
 public class + : ScriptableObject
 {
     [TextArea(2,6)]
     [SerializeField] string question = "Enter new question text here";
     [SerializeField] string[] answers = new string[4];
     [SerializeField] int correctAnswerIndex;
 
     public string GetQuestion()
     {
         return question;
     }
 
     public string GetAnswer(int index)
     {
         return answers[index];
     }
 
     public int GetCorrectAnswerIndex()
     {
         return correctAnswerIndex;
     }
 }
 
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using TMPro;
 
 public class EndScreen : MonoBehaviour
 {
     [SerializeField] TextMeshProUGUI finalScoreText;
     ScoreKeeper scoreKeeper;
 
     void Awake()
     {
         scoreKeeper = FindObjectOfType<ScoreKeeper>();
     }
 
     public void ShowFinalScore()
     {
         finalScoreText.text = "Congratulations!\nYou got a score of " + 
                                 scoreKeeper.CalculateScore() + "%";
     }
 }
 
 
 
Comment

People who like this

0 Show 0
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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

134 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

Related Questions

How can I select object in hierarchy in Unity via script? 3 Answers

How to display currently selected GameObject in hierarchy in custom EditorWindow? 2 Answers

UI Elements do not become selected? 0 Answers

Multi Select Button 0 Answers

Store clicks in sequence 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