Day 4: Unity quiz grade average

Jake Boere
2 min readApr 28, 2021

Today in Unity we’re going to code a quiz average calculator. For the quizzes we’re going to use random values each time we run the program. To start we will make variables for 5 different quizzes. What I did was create a different variable for each quiz you have, but you can also do it all in one variable just use one line and for each name of the variable just use a comma then write the next name and continue until you have 5. You will also want a variable for the quiz average. Once you’ve done that you should have something like this:

Next is creating the values for the quizzes to do that we need need to learn how to use the function Random.Range. This function in this situation allows you to crate a random value from a minimum and maximum value since we are working with grades we know the lowest value will be 0 and the highest will be 100. We will have to do that for each different quiz since we want each quiz to have different grades. After all that you should have something that looks like this:

Now we need to know how to calculate the average of all the quizzes and to do that we just add up all the quizzes first and then divide that by how many quizzes there are. So you write the quiz average variable you made equal to all the quizzes added together in brackets so that is calculated first then for this example we will divide the total by 5. You code should look something like this:

Lastly, we need to display this in our console so we know the code works. So, we need to use our Debug.Log again then add our message and we should see the average in the console and each time you run you game you should see a different average each time.

--

--

Jake Boere

I am hard working Game development student learning as much about game development and unity as possible.