Trivia Question System in Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody and welcome to this Unity tutorial on how to make a trivia system I've themed this one after Who Wants To Be A Millionaire but any trivia game will work with this as long as it's a question followed by a set number of answers in this one we have four answers with one question being asked and with this we're going to have it set up so that way we can ask as many questions as we want I'm not going to have it show that part but the question itself so any system you want to wrap this into that should be able to be added to so we want to make sure that first we have of course our questions on each one of these questions we made them a button and right now they don't do anything they just will Flash the highlighted color once I put the mouse over them or the selected color if I'm using the arrow keys or the controller and the Press color will be the gray one I click on it but we're going to want to start off with a script and I have these scripts on another screen here I'm going to be pasting in some code and we're going to go over that so we want to have the first script be the question data and this is going to be a scriptable object where we're going to just have all the parts that we need for our questions in this example we're going to have let me just paste in this class here first we're just going to get rid of start and update and we don't want this to be mono Behavior instead this is going to be scriptable object which in this case I'm just going to paste write it over this so we're going to have the question itself and that's going to go inside of our top box that's going to be the question text we're going to have the category and that's going to be that little box above that says category just the text there and this last one is going to be our answers I'm going to save this script changes and we see this tooltip here so the correct answer should always be listed first so the way that I'm setting this up is the correct answer will always go inside of the one that a will have but we're going to then randomize which position these are in and we'll go over that in a little bit but with this here our question data now that we've added this along with one more thing that I forgot to show on here we need to be able to create these scriptable objects so to do that right above our script name we're going to include create asset menu and we need to give it a file name in this case I'm giving it question and then the menu name I'm just using scriptable object slash questions so we can really know it's a scriptable object and the question matches what it is in this case questions anything order to one so if you had more of these you could set which order they're in I'm going to save that again and now when the scripts reload I can go over here and I'm going to be putting all these inside of the resource folder resources and then questions and we see that there's already one in here but if I go to create the top here we see scriptable objects and then we have a question and once I create one of those I could just say test and we see that the top now we have question test with a question mark category test and then we have our answers so we can add four of them we can click the button here to add one or we could just click in this box and change it to four to add four at once so I'm going to say correct answer because we know the first one should be a correct answer based on the format I chose and then we're going to say wrong one wrong two and wrong three so this would be any question that you want if you wanted more answers in here you would need more boxes on screen but this should be able to fit a certain number of them that you're looking for going back here we're going to need another script and this next one it's going to make it so our answer buttons are going to have the information that they need to be set up right now they just say answer text we're not going to be changing A B C or D we're going to be changing just the text inside of there and I'm using text mesh Pro if I open up one of these buttons here we see the letter and then the answer text and the answer text I just have it set to Auto size so that way it'll automatically adjust depending on how many letters are in here just so that way I don't need to worry too much about what size I'm making the actual text in there it's going to adjust for me so let's go back to our scripts folder and this next script we're going to say is answer button so under question data we're going to need to add a using TM Pro for me because I'm using text mesh Pro if you're not you don't need to add that we're going to add a couple Fields here so I'm going to take away our start and update this is just going to have information passed to it and it's also going to have a function when we click on it so to start off we want to know if this button is the button that is the correct answer because once we've randomized those it's going to change from the first one so we're not always going to know this is going to be passed in from a different script so the next field we have is just the answer text that it's going to be changing on screen the first function I've brought in here is set answer text so it's going to get text from another script and then it's going to be told what the text should be that's going to be setting that we also need another function in here to then set since we have a private field we want it to set the is correct Boolean and we're just giving it a new Boolean value and it's being set to that new value if you made these public you could leave it like that I like to make things private when I don't need them public and the last thing we're going to need in the script is we're going to need our on click function so whenever we click on this button we just want to check is this correct and I'm having it debug the words correct answer for correct and wrong answer for wrong answer and this is where you would put your logic into say a scoring system or whatever else you would want in here we're just going to use the debug to see that they're working so I'm going to save that I'm going to go back here now I made each of these buttons from one prefab so I'm going to be able to just go into my assets into the prefab open up my answer box and on here I can scroll down and I'm just going to add a component answer button let me open up that and we see that there is the answer text which if I go up here we see answer text let's drag and drop that right in and we also have our on click function if we don't set this it won't do anything we'll have all the scripts set up but it won't do anything so the object we're looking for is our answer button script and the function we're looking for is our on click under answer button with that set up we can hit save we can go back and now all of these should have it on it you're going to see a has that set up B has it set up and so on so those two scripts only say that here's the information that's going to be inside of our question and then this one is going to be how do our buttons interact with that so now we need one that's going to actually select which questions we're going to be using so in this one I called it question setup which I've clicked off of it after I made it so let me just delete that one I could rename it but I have to rename two spots and I'm just going to recreate it and we called it question setup opening up this script we're going to need a couple different things in here just like our first one our second one rather we're going to need text message Pro and I'm going to paste these in in small sections so I'm going to leave start an update for now and at the top we're going to add a serialized field for our list of question data so this is going to be our actual questions now when I say question it's not going to be just the question string it's going to be all of this as one object it's our scriptable object that we're using that's why it matches the question data name and not just the field name of string we're also going to have the one of them called current question so that way we're going to keep track of which question we're currently on and we're going to use that to get all the data from it without having to select it from the list multiple times and then also you're going to see how we're going to make it so that way this won't repeat questions while we're in the game it will repeat if you restart the game but while you're in the same session it won't repeat the next few ones is going to be what we're going to be actually setting up in this case our question text our category text and our answer buttons so these are both text mess Pro uguis and that's for our text but then answer button for our answer buttons and you're going to notice this is an array so we're going to have multiple of these yeah the last thing we're going to need here is a int for correct answer Choice since they're going to be moving around we need to keep track of which one of these is going to be our correct answer and we'll see that in just a moment so the first thing I want to do is I want to make sure that these things are being set up on a week no real particular reason for that other than I want to separate when I'm putting the values on screen and to start or you could put it in a different function depending on when it's starting in your game but I'm going to put this into a wake so when this scene loads it's going to get all the question assets so if we have get question assets inside of here we're going to have to make that function so I'm going to come down beneath update here and I'm going to add that function so in this one we're going to say our questions which matches our list of question data up there and we're going to set this to a new list so normally you would use resources.load all and in this case I'm telling it I want them to only find question data and then I want to inside of this path questions so the resources means it's going to load from our resources folder which we see here that's why I put them in this folder and then from that folder I added questions and that's going to be the path where it's going to put them and then to make this a list because this is an array this part from resources to question that's an array so to make it into a list we could just do new lists and then give it inside of our arguments of the function we can give it the array so that's all we need in order to load in all those questions and now we're going to go back to our start function I'm going to add in the other functions that we're going to want to do so the first thing I'm going to want to do is select a new question so to select a new question we're going to add a new function down here and we're going to say get a random number for which question we're going to pick from our list so just getting a random number index from a random range from 0 to the number of questions we have in this case questions dot count since it's a list if you have an array it would be length then we're going to set that question to the one that we picked so our current question is being set to questions with an index of that random question index after that we have our questions and we're going to remove that same one that we just picked so we're putting it into this current question and then we're removing that one from the list so that way if we have five questions and we pick the third one we're removing that third one from the list so that way next time when we pick from that list it's not going to be able to choose that one since we removed it that's going to solve it from repeating until the game is restarted since these aren't going to go away once we remove them they're going to still be there they're just not in this list so when you restart the game or play and stop or whatever you do it's going to bring it back into that list on this first one it's going to load it in here so after that we have set question values and that's where we want it to show on the screen that we have the text showing up so that function here we have question text Dot text equals our currentquestion.question so we have our question text is there a question and we have our category then afterward is being set to our category which is category text Dot text equals currentquestion.category so we're using that current question that we set here and we're just taking the information out of it and all this information is from our Quest question data right here we have our question and we have our category so the last thing we haven't used is our answers we'll see that next we just zoom in here to make sure this is big enough so we see that set answer values is the last one so I'm just going to scroll down here and I'm going to paste this one in as is but there's going to be a function inside of it so for setting our question values the first thing we're going to do is set random orders for our questions so we see we have this function here for randomized answers so we're going to have to add that it's going to be this so we have a list of string being returned and it's going to be randomized answers the same name and it's being passed a list I'm calling original list we're having a Boolean value for chosen answer the correct answer Chosen and that's going to be false now remember what I said at the very beginning in our question data the correct answer should be listed first that's where this comes into play back in our question data right here's where it's going to come into Playbook there in a sec so we're going to iterate through this list of all of the answer buttons because if we use the original list or the new list new list is going to be empty and original lists we're going to be removing them so we make sure we don't have any repeats so that's why we're using answer buttons so we know how many answers we're choosing and not using these lists as a length because otherwise it's going to shorten or already be zero and it won't do anything so first thing we're doing is just getting a random number between 0 and the original list.count so that way we know how many choices we have left to choose from and once we do we're saying hey is that random number zero because that's going to be the correct answer but since we can have multiple times be zero it's just the first time we want to make sure that it's the correct answer so that's why we have this Boolean for correct answer chosen so if this is the first time we're getting a zero our correct answer choice is going to be whichever iteration of our for loop we're on and then also we're setting our correct answer chosen to true that's going to stop it from choosing this again and setting our correct answer to multiple values after that we're going to add that one to our new list and then we're going to remove it from the original list once it's run through that list in this case four times for our four buttons we're just going to return that new randomized list which brings us back here now we just need to set up the answer buttons so we're just going to say for every one of our answer buttons from zero to our length of the over answer buttons first thing we want is is this one our correct answer we're going to initialize that as false so that way we only change it to true if we see that same value so that's what we see right here if I spelled it wrong if it is the correct answer set the bull to true so we're checking I is double equal to we're comparing it to our correct answer choice if it is we're setting it to true then all we need to do is well pass along those values which if we remember back here we have set is correct and set answer text which we see here set is correct to our value for is correct false if it didn't match drove it did and then we're also giving it answer text to answers and the one that we're currently on and with that that's going to randomize our list with this randomized answers and it's going to set the values here so we see that inside of start we have those three things that are setting it all up and we actually don't need update looks like I left it there for some reason either way so we're going to save all this we're going to go back to Unity and inside of here we're going to just put these functions where they belong I'm going to put them on our box question box so I'm going to say this should be question setup and we see we have our questions that's going to be loaded in I'm leaving it here as a serialized field so that way we can view them while in game just for debugging purposes our question text that's going to be under our question box question text I have it here now we need the category text I put the category underneath that box so we have our category text and then we have our answer buttons these are the four of them so I'm going to lock this which we could also right click and go into properties that'll just open up the separate window which actually I'll keep it with that one I'll uncheck that for locking it so now with this unlocks I can move this around anywhere I want I'm going to leave it here for now and I'm going to just select our four answers boxes and drop them right into answer buttons so now we see answer box answer box a b c d all the way down and our correct answer choice is also being set by the script but I'm just leaving it here for debugging purposes so we could see what that value it's chosen is so with all that and close that properties window that we brought up I'm going to save this and did we forget to add a script to anything this is on the buttons this is our data and then this is on the setup so with all that we should be able to press play and let's see if we're able to see a question show up in there and we do we see test with the question mark we see wrong two wrong three wrong one these are not in the order that we gave it so our randomize seems to be working if I go back here we see wrong one two and three so it's not in the same order correct answer happened to pick the same one now let's see if our debug line works when we click on these now we see two is wrong C is wrong and D is wrong and if we press a if we see the correct answer and we always want to test it multiple times because it could have been a fluke that it worked the first time so we just want to make sure we test a bunch of values here we see it picked the same question so if it doesn't always consider a right and we see that the answer so far seemed like it's working flawlessly Press Play One More Time see if we can't get it to load that other question answers still seem to be working and it's always grabbing just this question so I want to double check in my question box we see here questions it does have the other one as a value it just happens to be not choosing it I see it in the list that it's being added to and that's why I wanted it there because without multiple ones here sometimes it could seem like it's not doing it if I delete this one we should see that it picks the other one there we go which we see it just has the same answers it just has question in all caps instead of having a question mark so I also want to show you an easier way that we can get these questions to show up so in order to do this I'm going to use a CSV file which I've already got one here so it just Excel and at the very end just save it as a DOT CSV I believe it's going to say let me just pop it up CSV utf-8 comma delimited that's going to be the important part comma delimited so we see here that I just have question a category a correct answer a wrong one a wrong two and a wrong three let me say what is a sandwich it's under the food category ham and cheese is the correct answer ice cream cake and water are not correct answers so with this CSV file I'm just gonna I'll leave this up for a sec but inside of here we're going to have it so Unity is able to grab the values from that CSV file and automatically make those scriptable objects using the data that we gave it so we need an editor folder since it's going to need a unity and unity editor so this is going to be adding in unity editor so inside of this folder I have another folder for csvs in case there are multiple and I'm going to add a c-sharp script here and this one is going to be CSV 2. I'm going to do so for scriptable object so inside of here we're actually going to change out the top here we're going to be using Unity engine Unity editor and system.io the first thing I'm going to want to get is a question CSV path so this is going to be the path from the assets folder to where I saved it so in this case under editor then csvs and then it's going to be called question.csv we're also going to need the number of answers this is just going to be so we don't have magic numbers in our script later on and then we also need to make sure that this has none of these we don't need any of these and we also don't need mono behavior for this one so we're just going to remove that so we are going to need a menu item to know where we're going to have to be able to select this I'm just going to call it utilities and generate questions after this we're going to actually have our function which I'm going to paste in the whole thing so it's going to be a bit long but we'll go through it so we're going to start at the top here so I'm calling it generate this case I had phrases I'm going to rename this to questions there we go we're going to have it generate questions I did reuse this script from another project I was working on where it was phrases so I'm having a debug log just to make sure that it is running this called just saying generated phrases or questions in this case so we want to load in all the strings from that file or all the lines as strings so we have file.read alllines and it's application data path so that's going to bring us to our asset folder and then from there we're adding to it our question csvpath that's why this is after our asset folder which we see here if we have assets and then we have the file path we're giving it so this is going to give us wherever this is saved on this computer whichever computer it is and then it's going to add in the specific path we added this to after that we're doing a for each Loop so we're going to look through all the strings which we're just calling s in all the lines we just loaded in and now we're going to split that data so we're saying that we have a array of strings from the string being split now it's reading in all of them per line so if we remember here this will become one string split is going to bring it so that way at the commas it's going to separate them so we're just separating it into our different columns here okay and we see that our first column which I've written out here this is the format I'm using just that way it was in here as well the first one is our question so we need to make a new question data I'm just calling it question data as well and it's going to be scriptable object.create instance and it's it's question data so we're going to set the question because now we need to go inside of this script and we need to set up these values that we gave it we need to set question and category and answers and we're just doing them in the same order our question is going to be our first selection in that split data so that's our zero in the array zero index after that we're setting our category to number one which is the second value in that array then we're initializing our array of answers just to make sure that we have an array of length four so we have our question data.answer so we're setting it equal to a new string with a length of four we're going to iterate through those four in a for Loop starting at zero going through the number of answers that's why we didn't want a magic number earlier that's where we got a number of answers we're using it here and then for each one of these it's just going to be our question data.answers whichever one we're on zero through four in this case and we're going to say that data which is actually before force of zero through three so our split data and we're adding two we're doing two plus whatever number we're on so the reason we're doing that is our first two are already accounted for so we're starting here and because of the way arrays work our column A in this case will be 0 1 and then two so we're saying start here and because we're starting at zero with I that's going to be 2 plus 0 that's going to be 2 and then from then it's just going to be the next column over and that's going to keep getting us all the way across in this case only these four after that we want to make sure we give it a file name now there's some characters that won't work in strings it'll give you an error if you try to do it if you see that error and make sure that you're not using a character that's not allowed in the name in this case I have question marks and that's because our questions have question marks in them so we see this one if I let that be the file name it would return an error and not let me use it so I'm saying if it contains a question mark I'm going to set the name of this question data object to the same name as the question but I'm going to remove the index of the question mark and you could do this for any other thing that you need to make sure that it doesn't have a name I think quotations are one of them and there's a couple other things if you have an error with this part double check that you're using only characters that are allowed inside of names or files after that I'm just double checking that say I didn't include the question mark like this first one here there's no question mark in there and it might try to set it to nothing because this only does something if it finds a question mark so we want to make sure if it doesn't find anything it doesn't change anything so we're just bringing in the question as is after that we're saving this to the resources folder by doing assetdatabase.createasset we're giving it the object we just created and I'm using this dollar sign here to make sure that we can add in this value here we're having without having to concatenate the strings by doing plus that name and all the other stuff so it's just a way to drop it in there quicker we have our assets and then we're going to resources and then questions and that's going to be this folder here our resources and our questions it's going to bring it into here so if I delete this here we have nothing inside of here and the last thing outside of this because all of this was in a four each the last thing we need to do is save our assets so with all that I'm going to save this file go back to Unity and let it load for a sec we see there are nothing inside of our questions if I look inside of our editor here's where we have this script which I'm going to include a comment on that one to make sure it goes inside of the editor folder when it's in the GitHub which there's going to be a link in the description and we have our CSV folder inside of there with our question CSV so what this did was at the top we have this new utilities and you could have named this anything you wanted in this case I named utilities then generate questions if I click on this we see I got an error so let's take a look at what that was it says there is an error with this path let's see what this could have been questions.csv oh let's take a closer look at that error sharing violation so the reason that there was a sharing violation is I still have this open it can't be opened by me and then Unity can't open it as well it can't be open twice so I'm just going to not save my changes since I don't believe I made any that made any difference anyway and then now I'm going to clear that and go to utilities and generate questions again and we see this time it just says generated questions so it should have worked if we go to assets and we go to resources then questions we see there's two questions here and this question category correct answer one two and three and then what is a sandwich under food category ham and cheese ice cream cake and water if we go back to our CSV file but you might just click on it here we see those values up here they're in the same order so it generated those questions and if we double check their name the name of this file is question and this one is what is a sandwich with no question mark so it's able to generate those and then now if I just press play since it's loading them in from that file without any other assistance from me it was able to pick the question let's first play a couple times here and see if we can get it to pick the other one there we go and we see it was able to pick either of the questions and it is random so it could be biased or picking one for a little while but it should statistically pick the other one eventually and then also if you plug this into a system where you are making multiple questions you would just need to have it call the function for select new question whenever you wanted a new question and that's already set up to randomize all the values and then on here you would just have to tie this to well which one's the correct answer I picked this one I should get maybe a point or something I should make a noise as well when you click on it depending on whatever you're doing with the game but this is how you could set all of this up and be able to change it very quickly using a CSV file either Excel or whatever spreadsheet software you want to use and that way it's able to import load automatically and the only thing you have to do is come up with your questions so all of this is going to be on a GitHub I'm going to have a link in the description for that below so I hope you learned something from this and I will see you guys in the next video thanks for watching
Info
Channel: Jayometric
Views: 4,750
Rating: undefined out of 5
Keywords: Jayometric
Id: EhG7K3c3IjA
Channel Id: undefined
Length: 29min 28sec (1768 seconds)
Published: Sat Jan 21 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.