Trivia Question System in Unity Tutorial

Ойындар

Scripts on Github: github.com/Jayometric/TriviaS...
Subscribe! ► / @jayometric
Check out my other social media and games! ► jayometric.carrd.co/

Пікірлер: 75

  • @andreu8541
    @andreu85412 ай бұрын

    quick tip: you can also use a tsv (tab-separated values) file instead of the csv. this will be useful in case any of your answers/questions have commas in them, otherwise it will generate more splits than needed or expected. you only need to do this changes on the CSVtoSO.cs: (line 13) private static string questionsCSVPath = "/Editor/CSVs/Questions.tsv"; and (line 25) string[] splitData = s.Split('\t');

  • @jayometric

    @jayometric

    2 ай бұрын

    Good idea! I didn't have any questions that had commas but that's a great solution! Thanks for sharing.

  • @Jonesthecasey

    @Jonesthecasey

    5 күн бұрын

    Hello, I am getting error at implementing tsv file to my project. I am getting an error of: UnityException: Creating asset at path Assets/Resources/Questions//"This feature displays the tabs, indents and margins that give users a visual guide for allignment.asset failed. CVStoSO.GeneratePhrases () (at Assets/Editor/CSVtoSO.cs:70) The "this feature displays the tabs," part has the comma. The syntax at Line 70 is: // Save this in the questionsPathfolder to load them later by script AssetDatabase.CreateAsset(questionData, $"{questionsPath}/{questionData.name}.asset");

  • @andreu8541

    @andreu8541

    5 күн бұрын

    @@Jonesthecasey judging by what you sent i think it has something to do with the asset generation. On the error you send it says that the path is [...]/Questions// and i assume that those to slashes at the end shouldnt be there like that, there's something missing in between. Make sure its generating properlly the file's name for each question, otherwise ive got no clue, sry.

  • @vaxtrixradicalbro4295
    @vaxtrixradicalbro42952 ай бұрын

    Such a great video! If it wasn't for this video, my whole internship project would've failed.. TYSM!

  • @jayometric

    @jayometric

    2 ай бұрын

    Glad it helped!

  • @JoaoPortal14
    @JoaoPortal14 Жыл бұрын

    Thanks a lot, this video was very helpful!

  • @fulgencejuniorlohore854
    @fulgencejuniorlohore8544 ай бұрын

    Thanks buddy for this nice and useful tutorial! 2024 and still relevant!

  • @jayometric

    @jayometric

    4 ай бұрын

    Glad it helped!

  • @fred1541
    @fred15413 ай бұрын

    Yes, As a student, I really want to learn how to make this kind of game for turning in my homework, thanks~

  • @Wynsol
    @Wynsol3 ай бұрын

    Very Nice tutorial Thank you 😊

  • @Wynsol
    @Wynsol3 ай бұрын

    Thank you 😊

  • @eunoah
    @eunoah2 ай бұрын

    My question now is. How do i call the events "SelectNewQuestion, SetQuestionValues and SetAnswerValues" from QuestionSetup, from "if (isCorrect)" to load a new question instead of debuging... Ps: I learned so much from this one "simple" video. Thank you a lot!

  • @jayometric

    @jayometric

    2 ай бұрын

    Glad you learned a lot! The basic idea was these methods could be used from these or another class. Instead of debugging text in if (isCorrect), you could put a score, an animation, whatever you would like it to do when you get the answer correct. Since the next question will likely be loaded after checking if the answer was correct or not, that's why it's after that section under if (questionSetup.questions.Count > 0), which checks if there are still more questions to choose from before generating a new question with questionSetup.Start(); If you only wanted it to load a new question after getting the answer right put questionSetup.Start() inside of if (isCorrect) and only there. Then it will only get another question after getting the answer.

  • @eunoah

    @eunoah

    2 ай бұрын

    @@jayometric I keep getting two errors: Once at the beggining with null reference exception about questionText.text = currentQuestion.question; from QuestionSetup.cs And the other when i click a button also null reference exception about the if statement at the end of the AnswerButtons.cs

  • @Distriks
    @Distriks4 ай бұрын

    such a good tutorial, you shou´ve gotten way morw views, i belive you didnt because of how long the video is and the thumbnail could be better

  • @user-es9jy9tm6p
    @user-es9jy9tm6p3 ай бұрын

    Hello, this has been very helpful for a school project, so thank you! I wanted to ask tho, if there is any way that the amount of buttons that appear on the screen could change depending on the amount of answers set on the SO of every question. Im working with different types of questions that don't necessarily need four answer buttons, so how could I add this? Hopefully I explained it correctly.... Im a beginner so I would really appreciate your help!

  • @jayometric

    @jayometric

    3 ай бұрын

    I don't have my computer with me currently but there's a lot of ways that could solve this problem. My thoughts are to have buttons ready for the maximum number of answers and assign them in the inspector on the QuestionSetup script. Then you could adjust the QuestionData scriptable object to include a public int numberOfAnswers. Then in the QuestionSetup when we fill in the answer buttons with text, we could also turn on or off the extra / missing buttons. Inside the SetupAnswerValues we could do something like, if(i > currentQuestion.numberOfAnswers) { answerButton[i].SetActive(false); } This way we would turn off the buttons that are not needed since the question has less answers. Because we'd be turning off buttons we'd want to make sure we're doing the opposite as well, just in case the next question has more answers than the previous. Something like this, if(i

  • @unexpectedpause9359
    @unexpectedpause93596 ай бұрын

    How can i add a points every time i got an answer correct? thank you for the insight

  • @jayometric

    @jayometric

    6 ай бұрын

    The easiest way is to either add a score variable, like int score; to the QuestionSetup script or another object with a score script. Then in the AnswerButton script add a reference to the QuestionSetup (or the score script) and add to the score by accessing the variable directly (if it's public) or by making a function to increase the score like this: public void AddScore(int scoreToAdd) { score += scoreToAdd; } You would need to set the QuestionSetup (or score script) in every button but there are only 4 in this example so it won't take long. If there were a lot more answers to choose from I would set it up differently. I hope that helps!

  • @h2fortnuttv409
    @h2fortnuttv4094 ай бұрын

    Hello, first of all nice tutorial, i do have a question, when i click on an answer even if its bad or wrong, i don't go to the next question, how do i fix that?

  • @jayometric

    @jayometric

    4 ай бұрын

    Hi thank you! I left it open as it can change depending on the way it's added to different games but the simplest way is to: 1. Change the void Start() function in QuestionSetup to be public 2. In AnswerButton, inside of the OnClick() function we can add if (questionSetup.questions.Count > 0) { // Generate a new question questionSetup.Start(); } By calling the questionSetup.Start() it will select another question, set the values and set the answers again. If you're just using the UI to click buttons that should be all you need. You can find these in the Github link as well. Hope that helps!

  • @h2fortnuttv409

    @h2fortnuttv409

    4 ай бұрын

    @@jayometric thanks i’ll try this

  • @karauhlinger1774
    @karauhlinger17749 ай бұрын

    Hi a question for you. It seems that I can't get a new scriptable object to save in my assets/resources/questions folder and my csv file wont save in the csvs folder. any tips on why that wouldn't be working?

  • @karauhlinger1774

    @karauhlinger1774

    9 ай бұрын

    update i manually uploaded the csv file but it still wont save as a new object in the questions folder

  • @jayometric

    @jayometric

    9 ай бұрын

    Hi, I just took some looking through to see what it could be. The way I had it set up required the folders to already exist and be set up exactly like this: The CSV files go in Assets → Editor → CSVs (easiest to save by dragging an existing CSV file from somewhere else into the folder, you can right click and Show in Explorer on PC, probably Finder for Mac) Then the folders where I have the Questions generated also needs to be: Assets → Resources → Questions. If you wanted to use a different folder structure you can, 1. Edit the questionsCSVPath in the CSVtoSO file on line 13 to change where the csv file is located. 2. Edit the AssetDatabase.CreateAsset path on line 53 of the same file to change where questions are generated. Remember, these were placed in the resources folder to make the QuestionSetup file able to access them easily (using Resources.LoadAll) so I wouldn't change the output directory personally. I edited the github file to make the path where the questions are generated to be a variable at the top to make changes easier (new line 14) and also to include a check if the file directory exists for generating questions, if it does not it will create the folder for you. It does not make a folder for the CSV as it needs to be in the project to be used anyway. I hope that helps!

  • @Jonesthecasey
    @Jonesthecasey28 күн бұрын

    I got a question, and its a good one. I want to have a Multiple different Scenes of Categories with a set of question. (Example: Scene 1 Math, Scene 2 English, Scene 3 Science). So this syntax must be changed at CStoSO. public class CVStoSO { private static string questionsCSVPath = "/Editor/CSVs/Questions.csv"; private static string questionsPath = "Assets/Resources/Questions/"; I want to change the questionsCSVPath and questionsPATH for multiple separate categories for different scenes. Is it possible to manually change the string by setting it to public? Thank you!

  • @jayometric

    @jayometric

    26 күн бұрын

    As it is currently the path for the questions isn't shown anywhere on the editor. The Utilities menu adds the button to generate the questions but there is no other UI. For that you would have to make something more custom, I'm not sure how to do that off hand. You can change the question path in the script for easier changes even if it's not ideal. If you wanted multiple categories for questions and for them to load in you could leave the import path the same and adjust the CSV file to include a column for "Category". You'd need to add this to the QuestionData as well. Then when you import the questions you could do anything you'd want. My initial thoughts are, when loading the questions, to start with removing all of the categories you don't want. Then that scene will only have the questions for that category. Setting up some boolean values or maybe an enums. If you wanted multiple categories together booleans might be easier to set up, since they would act as toggles. Just for example, Add 3 booleans, 1 for each category. public bool useMathCategory, useEnglishCategory, useScienceCategory; Add a new function, I'll just name it RemoveUnusedCategories. // Removes categories passed in as properties. private void RemoveUnusedCategories(bool usingCategory, string categoryName) { // Loop through all of the questions foreach (QuestionData question in questions) { // If not using the this category's questions, remove them if (!usingCategory) { if (question.category == categoryName) { questions.Remove(question); } } } } Then we use that function before we SelectNewQuestion in Start. Such as, // Remove question categories that won't be used RemoveUnusedCategories(useMathCategory, "Math"); RemoveUnusedCategories(useEnglishCategory, "English"); RemoveUnusedCategories(useScienceCategory, "Science"); I didn't get a chance to test it yet but something like that should get you started in the right direction. The basic idea is by adding a category column to the CSV, you can separate them in almost any way you like. The way I suggested keeps them all imported the same way, but changes how the questions are picked (by removing categories based on the name in the CSV). ? Just as a side note, I avoided using an enum for the category, that was so I could mix and match what categories could be paired (or all 3). And enum would make it so you are only allowed to choose 1. You would set those values on the scene itself so changing the scene would also change the selections. I hope that helps!

  • @Jonesthecasey

    @Jonesthecasey

    26 күн бұрын

    @@jayometric I see, boolean would make it more easier. I will try to experiment on this. Thank you so much for the help! I have subscribed :D

  • @Jonesthecasey

    @Jonesthecasey

    6 күн бұрын

    @@jayometric Hello, I got an update. I got your syntax working just today, sorry for the late reply. There was an error at the RemoveUnusedCategories function. So I have fixed it right about now and it's finally 100%. What I did what added a ToList at the foreach string. It looks like this. private void RemoveUnusedCategories(bool usingCategory, string categoryName) { // Loop through all of the questions foreach (QuestionData question in questions.ToList()) { Also I used the boolean values and integrated it to the main menu with a saving function via Playerprefs since the project doesn't need a complicating save/load system. The code is perfect, I already made 10 different subjects with it. Only problem is now how I convert CSV file to TSV instead since I can't use commas in Question.csv file. I'll post this as future reference in case anyone needs a fix to the issue. Thank you!

  • @JessieA.I-cb2uq
    @JessieA.I-cb2uq4 ай бұрын

    If we want to add lot question ex. 10,000 then how to do. Manually typing is time consuming right any other way?

  • @jayometric

    @jayometric

    4 ай бұрын

    As it is in the video the spreadsheet part makes importing them to Unity faster, the only way to write them faster I can think of right now is copy and pasting them from another source. But they would need to be entered manually if they aren't being taken from somewhere else. You might be able to import them a different way but offhand I'm not sure how.

  • @felaniiqhwan4769
    @felaniiqhwan47697 ай бұрын

    Hey man, I need some help. How do I generate a new question after player click the answer? I try to create a new function, no red marks but the game doesnt generate a new question

  • @jayometric

    @jayometric

    7 ай бұрын

    Hey sorry for the delay, I saw this late and had to recreate this project to test. To generate a new question after answering the question would be to add to the AnswerButton script's OnClick function. The if statement is already checking if the button pressed was correct, we just need to add a way for us to reference the QuestionSetup and call Start again (though you could make what is inside of start it's own function). At the top of AnswerButton add a reference to QuestionSetup, I did public QuestionSetup questionSetup; Then inside OnClick, after the if else statement I added: questionSetup.Start(); Then go back to QuestionSetup and add public before void Start() since it is not public by default. You could do it differently but this is way changes very little with the code and how it's shown on github. I hope this helps!

  • @jayometric

    @jayometric

    7 ай бұрын

    And as a side note, this also will break when you run out of questions! Set the questions list to public in the QuestionSetup, then you can use: if (questionSetup.questions.Count > 0) { questionSetup.Start(); } That will stop it from generating a question when the list runs out. Since every time we choose a question we remove it from the list.

  • @aliyylmn

    @aliyylmn

    6 ай бұрын

    Di saya belum berhasil sir, please , I need some help..@@jayometric

  • @aliyylmn

    @aliyylmn

    6 ай бұрын

    I haven't succeeded sir, please, I need some help..@@jayometric

  • @aliyylmn

    @aliyylmn

    6 ай бұрын

    I haven't succeeded sir, please, I need some help..@@jayometric

  • @miguelcarrizo3825
    @miguelcarrizo382525 күн бұрын

    Hi! how can i get a new question after pulse any of the buttoms? Really thanks for the video, it is amazing!

  • @jayometric

    @jayometric

    25 күн бұрын

    Thank you I'm glad you like the video! The basic idea is QuestionSetup controls almost anything about picking questions, but the AnswerButtons script has the button functionality. That's where the last section, OnClick() for the buttons comes in. // Get the next question if there are more in the list if (questionSetup.questions.Count > 0) { // Generate a new question questionSetup.Start(); } Calling questionSetup.Start(); does the following //Get a new question SelectNewQuestion(); // Set all text and values on screen SetQuestionValues(); // Set all of the answer buttons text and correct answer values SetAnswerValues(); Depending on your setup, you can just use Start or call them individually if you needed them somewhere else for some reason. This setup also implies there are more questions to pick from. If you have 0 left in the list (it removes the question it picks in SelectNewQuestion() I hope that helps!

  • @miguelcarrizo3825

    @miguelcarrizo3825

    23 күн бұрын

    @@jayometric i dont know why is not working :,c i have 20 questions but just first is showing in the ui, in the panel and QuestionSetup gameboject the questions are showing too

  • @miguelcarrizo3825

    @miguelcarrizo3825

    23 күн бұрын

    @@jayometric i'm doing some test and in questionSetup i made serializedField this variable currentQuestion to see it in the question box and it still being the same after i click on any buttom

  • @penguine9111
    @penguine91116 ай бұрын

    How can I change the code so that the questions changes , every time my game object interacts with my trigger it will show the new questions the code u gave will only change question once the game restarts , I need help its for my school work

  • @jayometric

    @jayometric

    6 ай бұрын

    It does depend on what is causing the trigger, but this only set up the structure. To make a new question when you interact with a trigger you would need to call the start function on the questionSetup since that selects a new question and sets the values. This could go on your trigger when interacting with it, or if you want the questions to only change when picking an answer it can be added to the AnswerButton OnClick function. I've edited the github file to reflect this on the AnswerButton. Ultimately wherever you want a new question will only need to reference the QuestionSetup and then call the Start() function again. Hope that helps!

  • @penguine9111

    @penguine9111

    5 ай бұрын

    @@jayometric why does it say questionSetup.questions is inaccessible due to its protection level

  • @jayometric

    @jayometric

    4 ай бұрын

    KZread does a great job not sending notfications sorry. I seem to have left out you need to make the Start function public. I hope you've found the answer but now but I wanted to leave an answer here in case others read this too.

  • @brahmantioputraumar3044
    @brahmantioputraumar30446 ай бұрын

    how can i change the option with image? is that possible with this method?

  • @jayometric

    @jayometric

    6 ай бұрын

    Depending on which image you're trying to change (the background, the question boxes etc) you would just need to add which image you want it to be changed to in the set question values function. If you wanted a unique picture for each question you'd probably want to edit the QuestionData scriptable object to include a picture, though you'd have to set this for every question. Not a big deal but if you try to access this without setting one it will have an error when the game plays. At the top of the QuestionData script add public Image questionImage; (this could be any name you want, depending on what you're changing you could make it backgroundImage or anything really) Then inside QuestionSetup we can add a new Image variable, say it was for the background: private Image backgroundImage; Then, in the QuestionSetup you can add to the SetQuestionValues method to include backgroundImage = currentQuestion.questionImage; (make sure it's the same name as before if you named it something else) Then you'd have a new image for every question, whichever part you replace could use that. If you want it to change the background you'd reference the image and then change the Image with something like: background.image = backgroundImage.image; As long as the background is set as an image this would swap them when the question is set up. I hope that helps!

  • @brahmantioputraumar3044

    @brahmantioputraumar3044

    6 ай бұрын

    @@jayometric tysm thats helping me alot. but i have more question. how can i limit the count question to show up? example i have 50 question but i just want 10 random question to show every program run?

  • @jayometric

    @jayometric

    6 ай бұрын

    Sorry for the delay, I don't know why it didn't notify me of this weeks ago. You could either make it randomly pick 10 out of the 50 in a new list and then use that list for the questions to pick from there, or you could have it remove 40 randomly from the list on start. Every time the scene restarts it will have the original 50 again. Hope that helps.

  • @AzaiKang
    @AzaiKang2 ай бұрын

    How can I loop the list? sorry for asking a basic question. I'm more of a story writer than a programmer, thank you in advance

  • @jayometric

    @jayometric

    2 ай бұрын

    No worries, it might seem basic but it can change a lot depending on what you're looking for. In the SelectNewQuestion method in the QuestionSetup.cs script we have the questions choosing a random value, then setting that to the current question. The last part removes that question from the list so it doesn't accidentally repeat a question. If you wanted to loop through them randomly without caring about repeats you can remove the questions.RemoveAt(randomQuestionIndex) line. Then it will repeat randomly until you close the program. If you wanted the list to repeat in order, you can replace the int randomQuestionIndex with a value we declare outside the method probably called currentQuestionIndex. Then set currentQuestion = questions[currentQuestionIndex] After that line we need to increase the currentQuestionIndex (so the next question becomes 1, 2, so on). For that we can add currentQuestionIndex++; We also need to stop removing them but also know when to repeat. For that we just need to add an if statement to check when our index >= questions.Count (how many questions in the list). That would make it follow the same order. To simplify: *somewhere at the top, around the other variables like private int correctAnswerChoice; Add: private int currentQuestionIndex; Change the SelectNewQuestion method like this: private void SelectNewQuestion() { // Set the question to the current question index currentQuestion = questions[randomQuestionIndex]; // Increase the question index so the next time this is used the value matches the next question currentQuestionIndex++; // Check if the questions should loop back to the beginning (0) (>= Count means we have hit or gone over, checking over just in case) if(currentQuestionIndex >= questions.Count) { currentQuestionIndex = 0; } } I hope that helps or at least get's you started in the right direction! I might be a little off with some syntax, I don't have Unity in front of me at the moment.

  • @AzaiKang

    @AzaiKang

    Ай бұрын

    @@jayometric thank you! that helps very much!

  • @miguelcarrizo3825

    @miguelcarrizo3825

    25 күн бұрын

    @@AzaiKang did you get it? it doesnt work for me :c

  • @Distriks
    @Distriks3 ай бұрын

    how can i show all the answers of the user at the end? to show him what he failed or got right?

  • @jayometric

    @jayometric

    3 ай бұрын

    We would need to make a way for the questions and answers to be stored. I would make a new script on a new GameObject, maybe named AnswerTracker. In this I would have a list of Questions, a list of Questions and a list of if those answers were correct. Something like this: [SerializeField] private List userQuestions; [SerializeField] private List userAnswers; [SerializeField] private List userIsCorrect; I would make a public method to add questions, answers and if the answer was correct to these lists, public void AddToTracker(string question, string answer, bool isCorrect) { userQuestions.add(question); userAnswers.add(answer); userIsCorrect.add(isCorrect); } Next, we need a way to get the question itself, right now it is private. You can just change it to public but I suggest making a Get method for this. Add a method to QuestionSetup such as: public string GetCurrentQuestion() { return currentQuestion.question; } Then we can add to the AnswerButton script, add a variable for the AnswerTracker such as: [SerializeField] private AnswerTracker answerTracker; Next, we need to call the method we made to fill in the questions and answers. We can use this in the OnClick() method. This can be anywhere in this method outside of an if or else statement. answerTracker.add(questionSetup.GetCurrentQuestion(), answerText.text, isCorrect); This will add the current question, the answer on the button selected and if the answer selected is correct to the AnswerTracker list. Then set these all individually on the 4 buttons by dragging the game object in the inspector. The last part is making UI that shows the questions and answers that were picked along with if it as correct. Just to make sure it works before making the UI I would add a debug to see all the info is working. Since it should display after all of the qustions are done we can add this to the OnClick method checking the questionSetup questions count. Inside the AnswerButton script OnClick method, add a section for if(questionSetup.questions.count == 0) { for (int i = 0; i { Debug.Log($“Question {i}: {userQuestions[i]}, answer: {userAnswers[i]}, correct answer: {userIsCorrect[i]}“); } } This is just one example of what can be done, there's plenty of ways to do it as long as you have the information stored to reference. I don’t have my computer available to try what I wrote out as it’s being repaired but I hope this is helpful! I may have some things the wrong capitalization but intellisense should hopefully fix those when you try it.

  • @Distriks

    @Distriks

    3 ай бұрын

    @@jayometric thank you so much, i will try all that tomorrow on what i am working on in my internship, and let you know how it went, thank you again for the fast response.

  • @Distriks

    @Distriks

    3 ай бұрын

    @@jayometric it worked very well, thanks

  • @Distriks

    @Distriks

    3 ай бұрын

    @@jayometric but to show the results in the UI I will need to make another code, correct? maybe named ResultsScreen. I'm thinking of making a counter to know how many questions i responded to, use the number to make a X amount of prefabs of my UI and then put all the information i got from your code into the prefabs, then get everything inside a scroll so i can see all the answers, what do you think? am i thinking correctly? and sorry if my English is bad.

  • @jayometric

    @jayometric

    3 ай бұрын

    I'm glad that worked so far! That should be okay, that can be done so many ways that it's probably best to try out the ways that make sense to you. If it ends up not working then you can see why it didn't and adjust the solution to the problem. With all of the data ready it's just figuring out what way you want to use it. Good luck with it!

  • @raimz2374
    @raimz23743 ай бұрын

    Can I put an image for the question?

  • @jayometric

    @jayometric

    3 ай бұрын

    Yes. Instead of using a string, use an image. Then between questions instead of changing the text, you can swap the Image.sprite to a different one. You should be able to change the scriptable object to have a picture instead of a string for the question, but you might have to set them manually since they won't be part of the excel spreadsheet if you were importing them from csv. Hope that helps!

  • @raimz2374

    @raimz2374

    3 ай бұрын

    @@jayometric Sorry I'm still new here, is it still possible if I only want some of the questions have image and the other don't?

  • @Mental_health_restart
    @Mental_health_restart28 күн бұрын

    What if i what to get the question from ai

  • @jayometric

    @jayometric

    25 күн бұрын

    Offhand I'm not exactly sure, you might need to find an AI to integrate, it might be a bit weird to get it to make correct and wrong answers. This setup worked under the assumption questions were already entered in a CSV. If you asked AI to generate questions then input them to CSV it would work. Not sure how well the AI will do making up wrong answers, check them if you try it.

  • @jenelynvilando3493
    @jenelynvilando34933 ай бұрын

    Is it possible to make this system into an application?

  • @jayometric

    @jayometric

    3 ай бұрын

    I don't see that being a problem. It will also work on touchscreens as it's just buttons and touch input works with those! The video sets up the framework so it can have things added around it (title and other scenes).

Келесі