Lists

Chris Didier
4 min readApr 7, 2021

--

A list is very similar to an array. A list is type that accepts multiple data types and we can extend and decrease the size at run time. A good use of this would be for an item database where you are constantly adding and removing items from it.

In the example below enemies to spawn is the list and objects to spawn is the array. Both are very similar the difference with the list is using <> instead []. It is also easier to add and remove from lists.

Next I’m going to do a challenge where I create a list of name and print out each one. When I hit the space key I am to remove a random element from the list and re-print the names. We even added in what name was removed from the list after each time the space key was pressed.

Now onto a bit of a more difficult challenge. It is time to create a list of 3 game Objects to spawn and randomly spawn within a random position on the screen between -10 and +10 on the X/Y when I hit the space key. Every object I spawn should be stored into a list called objects created. When I have spawned 10 objects, I will no longer be able to spawn objects and will turn all objects created green and clear the list.

This one had to be broken down into many parts to be able to solve. We knew from the beginning it was easier to select the array for how many types of game objects to spawn, because that was what was given to us. We also knew when the space key was pressed to spawn the objects between a random range of -10,10 on the x and y. So we created a new vector 3 and set x and y in previous variables to store them. We also stored the vector 3 into pos. To spawn an object we must instantiate it. Next we created the list for objects created and created a spawn count to set an upper limit of 10. Once the limit of 10 was hit no more objects would spawn would change color. If the player went to create an 11th object the objects would clear themselves out.

Finally I created an example item database on how to add and remove objects form it. First create a non mono script for your items this will include the item name, ID, and icon.

Next, we will create our item database script and empty game object. Attach the script to the item database game object. Create a list of item database from items. Create both add and remove methods.

Then create your player gameobject and player script and attach the script to the game object. Next create an array for the player inventory limit. This will be set to 10. We will also need access to the item database script so find the script. This will give access to be able to call the add and remove methods.

Finally, create the checks to make sure the items called and created are there and match what is in the players inventory.

Lists are great for expansion if you know your item list is going to expand over time it is a lot easier to work with than arrays. Arrays being fixed objects make it trickier, but if you know the size is always going to be the same arrays might just be the better option. Always adding tools to the chest.

--

--

Chris Didier
Chris Didier

No responses yet