Find specific object if they have same name?

I’ve currently got a game object with a collider attached to it. I have a bunch of enemies which all get instantiated with the same variable name of enemy. These are assigned as a trigger and when they pass into the collider get added into a List (via the OnTriggerEnter function) that keeps track of what enemies are within the collider. The problem I have is that because the number of enemies spawned is entirely random I can’t specify enough variables to start with to name them “enemy1, enemy2” etc and because you can’t dynamically assign variable names I can’t generate them on the fly either.

I realise I can use find the objects name however doing that is useless because they all have the same name. Is there a way I can “find” an object that returns a certain result from a method call?

My idea was when I spawn my enemy I give it a unique ID (pretty easy to implement) I can then pass that ID to my list and then when I need to interact with a specific object I can tell it to find the object that return the correct ID in question.

I figure I could just query them all as they would never be more than 50 enemies but it seems inelegant. Alternatively even if I find the name of the gameObject is unity smart enough to differentiate between the specific objects?

I hope that makes sense I can provide more information if need be. I have no specific code yet as I am trying to solve the problem with pen and paper before implementing it and I can’t find an elegant way to uniquely do this.

I’d love to be able to tag the enemies but it seems even in the latest Unity you can not do this through code… :frowning:

You can access scripts attached to game objects. See: Accessing Other Game Objects. Say I had a script called EnemyInfo as a component of my enemy prefab. An one of the public variables was an integer called ‘id’. In C# I might do something like this (unteste):

GameObject go = (GameObject)Instantiate(goEnemyPrefab);
EnemyInfo ei = go.GetComponent<EnemyInfo&gt(); = currId;

Now I have a unique id for each enemy I can store locally and reference later.