Instantiate for each in array

Hello! I’ll jump straight to the point: I keep an array of all targets in the scene. I want to instantiate arrow pointers that point to the individual targets position for each of the targets in the array. The array needs to update to keep track of spawning targets. I’ve messed a bit around with some code, and created this:

GameObject[] targets = GameObject.FindGameObjectsWithTag("AI");
        int targets2 = 0;
        bool hasSpawned = false;

        foreach (GameObject item in targets)
        {
            targets2++;
            hasSpawned = false;
        }

        for (int i = 0; i < targets2; i++)
        {
            if (!hasSpawned)
            {
                var f = Instantiate(arrowPrefab, transform.position, Quaternion.identity);
                f.transform.parent = gameObject.transform;
                hasSpawned = true;
            }
        }

My problem is that this is inside of Update(), and therefore instantiates a BUNCH of arrow pointers. But I can’t put it in Start because that doesn’t update the array. So yeah, say there are 2 targets in the scene, I want to instantiate 2 arrow pointers. Oh and if possible, say that 1 target dies 1 arrow pointer goes away. Arrow pointers are already finished btw.

Solved it myself using this:

    int previousValue;
    int value;

void Awake()
    {
        previousValue = targets.Length;
        value = targets.Length;
    }

void Update()
    {
        GameObject[] targets = GameObject.FindGameObjectsWithTag("AI");

        previousValue = value;
        value = targets.Length;

        if (previousValue != value)
        {
            for (int i = 0; i < targets.Length; i++)
            {
                GameObject arrow = Instantiate(arrowPrefab, transform.position, Quaternion.identity);
                arrow.transform.parent = gameObject.transform;
            }
        }
    }

Just checks if there has been a change in the “targets” array, and if so it runs a for loop.