How to find the varible of a script in a list made of gameobjects.

I’m trying to make a list of transforms and I gave every transform a script with an id. I want the list to read the ids while adding new gameobjects. If the new gameobject has an id in the list it won’t add.

What’s the best way to approach this?

private void DoSomething() {
int newId = 9999;
if (CanAddObject(newId)) {
// …
} else {
// …
}
}

    private bool CanAddObject(int newId) {
        foreach(Transform transform in listOfTransforms) {
            if (transform.GetComponent<YourScript>().ID == newID)
                return false;
        }
        return true;
    }