Unity UI: Multiplication Game

Ryan McCoach
4 min readJan 16, 2024

This article will cover how to use lists to store factors to multiply with a target number and remove them from the list when answered correctly.

CREATE LIST

To declare a List, you will need to use the List Class and pass in the data type of the list in the T bracket <>. Follow this with the name you want the list to have and initialize it.

In the Inspector you can populate the list since we gave it a Serialize Field attribute. In my case I have two lists that will hold factors from 0–12.

GENERATE PROBLEM

This method will either grab a factor from one list or the other and this is determined by a generating a random bool. Before pulling from the lists, I want to check if there is anything left in the lists. We can check the length of the list by using .Count. This will return how many items are currently in the list.

If one of the list is empty we simply grab from the other using the boo.

Picking a random factor from a list first start with getting a random number that will determine the spot in list we will pull from. This is the index.

The random number needs to be picked from 0 to the current length of list. This is important since we will be removing numbers from the list. This is the power of the lists; it is dynamic.

Depending on what lists we are getting the factor from, we set the first factor to this random number by accessing that spot in the list by accessing the list you want to grab from (_numList) and grabbing the number that is in the index spot of that list.

For example, if we accessed at index 3 (_numAList[3]) the number that is currently store there is 3. At index 8 is 8 (_numAList[8]) and index 12 is 12 (_numAList[12]).

We do same thing with the other list and assigning this factor from this list to the second factor in the multiplication problem.

Lastly, we multiple the random number from one of the list by the target number and set them to the Text elements on the screen.

CHECK ANSWER

Once the problem is setup, we are going to check if the answer the user inputted is correct or not. First, we declared a Input Field so we can grab what the user types into the text box and an Int variable to store the number that was typed in.

Assign the Input Field to script in the Inspector.

Using the int.TryParse method, we can convert the number that was typed in the Input Field from a string type to an Int type.

If the converted number that the user typed in is the same as the answer to the multiplication problem, we will remove that factor from the list using .RemoveAt(_index). This removes the item in a list at specified spot in the list.

You can see below the spot (element) in the list is being removed and the size of the list decreases as the user answers the problem correctly.

After the factor is removed from the list, we will check to see if both of the lists are empty or if the count of the lists are 0. If both lists are empty, it will be game over and if not we can generate a new problem.

All of this logic runs when the user input is correct and if it is not we can simply make the input field active again and tell the user to try again.

CONCLUSION

Lists are more dynamic compared to Arrays since you can add to them and remove from them. This showed how you can access an item from a specified spot and remove an item from a list.

--

--