List.Add() keeps looping

Hello, question is pretty much in the title.
Here is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class getColliderContent : MonoBehaviour
{
    public List<GameObject> objectsInCollider = new List<GameObject>();

    public string selection_mode;
    private bool done = false;

    void OnTriggerEnter(Collider other)
    {
        if(selection_mode == "harvest_trees")
        {
            if(other.gameObject.tag == "resource" && done == false)
            {
                done = true;
                objectsInCollider.Add(other.gameObject);
                Debug.Log("ONTRIGGERENTER");
                foreach (var x in objectsInCollider)
                {
                    Debug.Log(x.name);
                    x.GetComponent<Outline>().enabled = true;
                    x.GetComponent<resourceStateLog>().to_be_harvested = true;
                }
            }
        }
    }
}

Its supposed to put every gameObject in the collider to a list and then loop through the list and do some stuff. Everything works fine except for: “objectsInCollider.Add(other.gameObject);”

Sorry if i missed something trivial, thanks for any replies

Might I suggest changing the objectsInCollider list to be of type resourceStateLog instead of GameObject? The reasons are: 1) By assigning this component to the list, you don’t have to retrieve it any other time it needs to be used. 2) You can easily gather ALL resourceStateLog components with a simple call. 3) The game object is easily accessible from resourceStateLog. 4) The Outline can easily be accessed from it too.

Here’s a rework of the code with a few changes to the logic and layout, for a different perspective on the same idea:
<pre>public class getColliderContent : MonoBehaviour { public List<resourceStateLog> objectsInCollider = new List<resourceStateLog>(); public string selection_mode; void OnTriggerEnter(Collider other) { // by reversing the logic, we can remove the need for nested blocks of code making it a little cleaner looking if(this.selection_mode != "harvest_trees") { return; } if(other.gameObject.tag != "resource") { return; } // use a changed toggle to know if we need to do anything with the objects list bool changed = false; // a quick and easy way to find all components (and their game objects) we need resourceStateLog[] stateLogs = other.gameObject.GetComponentsInChildren<resourceStateLog>(); // loop through, skipping anything we've already added to the list for(int index = 0; index < stateLogs.Length; ++index) { if(this.objectsInCollider.Contains(stateLogs[index])) { continue; } this.objectsInCollider.Add(stateLogs[index]); changed = true; } // if we haven't changed anything, no need to do anything if(!changed) { return; } // loop through all items in the objects list, toggling state as needed for(int index = 0; index < this.objectsInCollider.Count; ++index) { this.objectsInCollider[index].gameObject.GetComponent<Outline>().enabled = true; this.objectsInCollider[index].to_be_harvested = true; } // we disable the trigger collider, rather than using done other.enabled = false; } }</pre>