Dynamically update the elements of a List

I have a list of game objects for which I calculate their distance from a certain point in the Update function while moving towards it. I want to add those distances in a List where e.g on list position 0 i will have the distance of the first object from the point and it will be getting updated while the object is moving towards it. I tried doing it with both the simple list.Add(distance) and list.Insert(0, distance) in the Update() functionbut it just keeps adding the distance of each frame in the list as expected. I tried using a coroutine but I can’t seem to find the right parameters for it in order to work.
Long story short, if I have 5 objects I need a 5-element-list where the distance of each object is stored in a specific place and it keeps getting update while the object is moving.

It should be as simple as:

MyList[0] = newValue;

I think I have an idea of what you’re asking. You want a list of values that change but instead you’re adding new items to the list.

Instead of adding to the list in Update() you should be updating the value in the list. So add the elements to the list somewhere else in your code:

Start() 
{
     list.Add(objectOneDistance);
     list.Add(objectTwoDistance);
     list.Add(objectThreeDistance);
}

Update()
{
     list[0] = objectOneDistance;
     list[1] = objectTwoDistance;
     list[2] = objectThreeDistance;
}

I just finished working on something similar that I think you could easily expand on for your needs.

I had a player object keeping a list of every enemy in a certain range by using an empty child object with box collider.

The child object gets a script to update the parent.

public List<GameObject> liveEnemies;
    private PlayerController PlayerController;
    // Start is called before the first frame update
    void Start()
    {
        PlayerController = GetComponentInParent<PlayerController>();
    }
    // Update is called once per frame
    void Update()
    {
        PlayerController.liveEnemies = liveEnemies;
    }
private void OnTriggerEnter(Collider other)
{
    if (other.gameObject.CompareTag("Enemy"))
    {
        liveEnemies.Add(other.gameObject);
    }
}
private void OnTriggerExit(Collider other)
{
    if (other.gameObject.CompareTag("Enemy"))
    {
        liveEnemies.Remove(other.gameObject);
    }
}

The PlayerController script has liveEnemies publicly declared inside it as well.

This will get you a list of the gameObjects with “Enemy” tag in range of the box collider, from there you can work out the distances you wanted.

As a side note, if an Enemy object is destroyed inside the collider it won’t trigger OnTriggerExit, the way I fixed this was a script on the Enemy.

public class EnemyInstanceManager : MonoBehaviour
{
    private sightManager playerSightManager;
    public int health = 10;
    // Start is called before the first frame update
    void Start()
    {
        playerSightManager = GameObject.FindGameObjectWithTag("Player").GetComponentInChildren<sightManager>();
    }

    // Update is called once per frame
    void Update()
    {
        // if health reaches zero, destroy object and remove from player sight list
        if (health <= 0) {
            playerSightManager.liveEnemies.Remove(gameObject);
            Destroy(gameObject);
        }