Have you ever arrived somewhere to do a bit of shopping and realized that you have forgotten your list? But then you decide “it’s okay I can remember it” and continue on with your shopping. If I had to guess, I would assume your experience would end up quite like mine and you end up buying half of the store and doing twice as much work than you probably should have…In our day to day lives a list can be a very useful tool that helps us to maintain a goal, track our progress, or even constantly evolving. One of the most import things about a list is you can change the size of it at anytime you want. Now what does all this nonsense have to do with programming? As a programmer we can use lists in the same way and often these can be more versatile than an Array even though they share some similar properties. So today lets take a practical look at how we can use a List<> and make an active targeting list.
Here we have 3 targets that we want to add to a list when they enter our target area. And then when they leave our target area we want to take them out of the list. Maybe we have some other code we want to implement like how to damage the target, or having the target fire back when its in our active target zone. This can get a little crazy when you have many targets in the target zone at the same time, and one of the best ways to handle this is with an active list.
So as our targets move towards the target area there are a few things we are going to need to make this work. We are going to need Colliders on all objects with is trigger so we can detect collisions, and at least one of these will need a rigidbody. It helps if you have gravity turned off on the rigidbody otherwise when you press play your object is going to fall straight down. We also need to write a little bit of code so our list can be implemented properly. Make sure to add “using System;” up top or you will not be able to use the list functions.
Here are a few of the basics to get you started with writing and initializing your list. followed by the code below of how to add and remove an object.
Now to make this work there are several ways, but for this example and to keep it simple I am using the “OnTriggerEnter” and “OnTriggerExit” methods. These are very handy and will detect collisions for you so long as you have an active collider with “Is Trigger” set to on. What this method is saying is “detect a collision and return the object I have collided with”. Now lets build a basic prototype to use this with and test it out.
As you can see for our prototype I am using a few different objects with a big slightly transparent green field so we can watch our object move through the target area. So keeping with our code here as soon as a target enters the green area we should see it in our list.
Ted has entered the target area and is added to our list
Ted has left our target area and now Bob is the active target.
Lets put this all together so we can see it in action. In order to see our list in the inspector make sure you remember to add [Serialize Field] if you are using a private variable as you should be. Now that we have the basic code down and our list is visible, lets give it a run.
Just for fun I added in a feature that will change our 3D object to green once it has gone through our target area just to make sure its working. Now if we only had the OnTriggerEnter function our list would look like below where all targets have been added. But that is not what we are trying to do with our list. So we add the OnTriggerExit and call ”.Remove” to create our active list.
And now we have a functioning target list that only displays our active target. Another example of this would be in a tower defense game as in the video below.
Here you can see that the enemy is fired upon once it is in a certain target radius, and it also fires back at the turret. I hope this helps to create a better understanding for one of the many ways we can use a list<>. Mahalo as always for reading!