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 AmISquidward · Jun 25, 2021 at 09:15 AM · scripting problemscripting beginnerscriptingbasicsquizvalid

Quiz Game : Valid Question keep decreasing each time the game start.

Hi there I'm making a true false quiz game. I have a problem regarding question index.


So there's supposed to be 30 question stored for a level, and the 20 question appear randomly one by one. After answering the 20th question then the player win.


But here's the problem. For example I start to try playing. The valid question is 30. Then I answer few question (for example 4). When I restart the game, the valid question is start from 26 instead of 30 again. Is there any way to fix it?


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
  
 public class QuestionsData : MonoBehaviour
 {
     public Questions questions;
  
     [SerializeField]
     private Text _questionText;
  
     public GameObject completeLevelUI;
  
     void Start()
     {
         AskQuestion();
     }
  
     public void AskQuestion()
     {
         if (CountValidQuestions() == 10)
         {
             _questionText.text = string.Empty;
             ClearQuestion();
             CompleteLevel();
             return;
         }
         var randomIndex = 0;
         do
         {
             randomIndex = UnityEngine.Random.Range(0, questions.questionsList.Count);
         }
         while (questions.questionsList[randomIndex].questioned == true);
  
         questions.currentQuestion = randomIndex;
         questions.questionsList[questions.currentQuestion].questioned = true;
         _questionText.text = questions.questionsList[questions.currentQuestion].question;
     }
  
     public void ClearQuestion()
     {
         foreach (var question in questions.questionsList)
         {
            question.questioned = false;
         }
     }
  
     private int CountValidQuestions()
     {
         int validQuestions = 0;
  
         foreach (var question in questions.questionsList)
         {
             if (question.questioned == false)
                 validQuestions++;
         }
  
         Debug.Log("Question Left " + validQuestions);
         return validQuestions;
     }
  
     public void CompleteLevel()
     {
         completeLevelUI.SetActive(true);
     }
 }



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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by SirCrazyNugget · Jun 26, 2021 at 03:09 AM

It looks like you're just counting questions which haven't been asked (which is probably right) and not resetting the value on restart. You'll need to iterate over the questions and set each question in questions.questionsList to true.

I'd rename some of your variables too, questions should be the list of questions questions.questionsList has ambiguity.

Personally, I'd address the way you're picking your questions and reset the array or available questions. This is only pseudo and won't tie up with your variable naming.

First create a list of all questions however you're currently doing it, then duplicate that list at the start of the first round.

 var availableQuestions = new List<Question>(allQuestions);

Remove the question from the list when it's picked.

 var index = Random.Range(0, availableQuestions.Count); //also ensure you haven't ran out of questions
 var selectedQuestion = availableQuestions[index];
 availableQuestions.Remove(selectedQuestion);

//display selectedQuestion

And separate out your functions to keep them concise as to what each does. e.g.

 public Question PickQuestion() {}
 public void AskQuestion(Question q) {}
 public void DisplayQuestion() {}
 public void RestartGame()



Comment
Add comment · Show 2 · 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
avatar image AmISquidward · Jun 26, 2021 at 09:35 AM 0
Share

Thankyou for your reply. This is my script to store all my question :


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [CreateAssetMenu]
 [System.Serializable]
 
 public class Questions : ScriptableObject
 
 {
     [System.Serializable]
 
     public class QuestionData
     {
         public string question = string.Empty;
         public bool isTrue = false;
         public bool questioned = false;
     }
 
     public int currentQuestion = 0;
     public List<QuestionData> questionsList;
 
     public void AddQuestion()
     {
         questionsList.Add(new QuestionData());
     }
 
 }



should I put the code here?

avatar image SirCrazyNugget · Jun 26, 2021 at 10:20 AM 0
Share

When you create objects you really want to make them like they are in the real world. While what you have is QuestionData with question, isTrue and questioned are all probably methods, I'd think of it more as a host reads the Question which has some text and an answer, thus

 [System.Serializable]
 public class Question
 {
     public string Text;
     public bool Answer;
 }

I'd possibly give it a topic too, or a number of points, data which is only specific to the question. When a host has read the question, he doesn't then mark it as read, it gets discarded, remove it from the array of available questions.

Then what contains the questions? The game, so

 public class Game
 {
     public List<Question> Questions;
 }

and probably inherit from MonoBehaviour for access.

Within the class you'd have methods to Restart a game, which should really handle the first play too (DRY principle), Pause? End a Timer? and then the Gameplay. If you write out each of the methods initially of what the game needs to do and then it's just filling in the blanks.

Before you know it you'll be adding multiple choice questions, images, multiple opponents and highest scores. Keep the classes to real world entities e.g. Game, Player, Timer, Question, etc and you'll be set.

avatar image
0

Answer by AmISquidward · Jun 26, 2021 at 01:55 PM

Thankyou for your suggestion. So I make it like a question countdown like this :


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class QuestionLeft : MonoBehaviour
 {
     public Text questionsleftText;
     private int questionsLeft;
 
 
     void Start()
     {
         questionsLeft  = 20;
         questionsleftText.text = questionsLeft.ToString();
     }
 
 
     public void MinQuestions()
     {
         questionsLeft -= 1;
         questionsleftText.text = questionsLeft.ToString();
     }
 
 }



and then I make it in my game manager like this :


 public void ShowResults(bool answer)
     {
         correctSprite.SetActive(questions.questionsList[questions.currentQuestion].isTrue == answer);
         incorrectSprite.SetActive(questions.questionsList[questions.currentQuestion].isTrue != answer);
 
         if (questions.questionsList[questions.currentQuestion].isTrue == answer)
         {
             questionLeft.MinQuestions();
             scores.AddScore();
             if (questions.questionsList[questions.currentQuestion].isTrue)
             {               
                 trueAnswerText.text = "CORRECT!!";
                 falseAnswerText.text = "WRONG:(";
             }
             else
             {
                 trueAnswerText.text = "WRONG:(";
                 falseAnswerText.text = "CORRECT!!";
             }
         }
 
         else
         {
             questionLeft.MinQuestions();
             scores.DeductScore();
         }
 
             trueButton.interactable = false;
             falseButton.interactable = false;
 
             StartCoroutine(ShowResult());   
     }



Then I put it on the game object and put the game object in the game manager than I try to play and it works. The countdown decreasing on every question like it supposed to do.


But the problem is when I change the game over condition. Before, it was like this :


 if (CountValidQuestions() == 10)
          {
              _questionText.text = string.Empty;
              ClearQuestion();
              CompleteLevel();
              return;
          }



but then since I'm using the countdown, I change it to be like this :


  public QuestionLeft questionLeft;
 
 public void AskQuestion()
     {
         if (questionLeft.questionsLeft() == 0)
         {
             _questionText.text = string.Empty;
             ClearQuestion();
             CompleteLevel();
             return;
         }



this happen instead :

Non-invocable member 'QuestionLeft.questionsLeft' cannot be used like a method.

Do you know any idea why?

Comment
Add comment · Show 1 · 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
avatar image AmISquidward · Jun 26, 2021 at 02:45 PM 1
Share

Ahhh never$$anonymous$$d... I should have use


 if (questionLeft.questionsLeft == 0)



and than change private int questionsLeft to public.

The game its pretty much done now. Only adding some BGM and SFX left.

Thankyou very much for your enlightenment. I am very grateful.

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

227 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 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 to implement tokenization of game characters? 1 Answer

Beginner Question: How to get normals from a physics raycast using visual scripting? 0 Answers

weighted inventory system 1 Answer

how to make a detect rotation script for negative rotation too 2 Answers

Quick code help! (c#) 2 Answers


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