Does anyone have a suggestion regarding this? I need to create an array with gameObjects with a specific tag, in this case “NPC” - which is very straightforward, but I then need to REMOVE all elements from that array where the vector3.distance is beyond a certain value (let’s say 20) - I’ve tried a few approaches but can’t seem to solve this second part
Option #1) “I then need to REMOVE all elements from that ArrayList where distance > 20”
ArrayLists are like fixed size, built-in arrays, except they’re semi-magical in that you can easily dynamically Add() or RemoveAt() elements from them, making your requirement simple to code.
Option #2) “I need to ADD all elements to another array where distance <= 20”
If using an ArrayList seems scary, another option is to use a second array.Set its size as the same as your original array, but create a counter variable that tells you how many elements are populated.
The basic goal of this is to cycle through targets - that bit I know how to do, it doesn’t need to update frequently, just when the function is called (it’s not in an update)
You need to cycle through the list backwards if you’re removing items from it. Otherwise you’ll be skipping entries (you first check entry 0, then remove it. The item that was at entry 1 is now moved up to entry 0; entry 2 moves up to entry 1. But now you choose to check entry 1, thus skipping the original entry 1) By going in reverse you avoid this issue entirely.