- Home /
Make an array/list of objects in enemy sight trigger
I know arrays/lists are quite a popular topic here, but nothing I have seen has helped with this so far. I want to make an array/list (accessible throughout the whole script) to store the enemies which are in a trigger.
I need an array which can be resized, or at least recreated with different length, but I don't know how to define the type of a resizable array outside of a function (so I can use the array throughout the script). Builtin arrays work as they can be defined out of a function but I can't figure out a way to resize them.
I want OnTriggerEnter to add an object to the array and OnTriggerExit to remove it, and I know how to do everything except create and use arrays/lists. If somebody could tell me the right type of array or list to use and how to implement it it would be much appreciated.
Answer by Slobdell · Jun 16, 2013 at 08:37 PM
Use List not array, it does exactly what you want.
List<typeOfObject> enemies = new List<typeOfObject>();
Then you can use the methods
enemies.add();
enemies.remove();
and your list will size itself automatically.
Steve
Thank you for your answer. Does that work in JavaScript too? I will use C# if I have to, but the rest of my project is in JavaScript.
Your answer
Follow this Question
Related Questions
Avoiding adding duplicate objects to ArrayList 2 Answers
Remove object from List after gameObject was destroyed in between Scene loads 1 Answer
A node in a childnode? 1 Answer
List all children in array? 5 Answers
Setting generic list length 2 Answers