I'm currently trying to solve a way to go about the following problem before I start writing any code (the old fashioned pen and paper way) but I'm not sure how I could achieve the following.

I'm going to have an object with a Collider attached to it in my scene. As my instantiated enemies pass through this Collider they will be added to an ArrayList which lets the object in question keep track of the enemies. The problem I can't figure out is how to determine which object is which.

I will have a script attached to my main camera which will be responsible for spawning the enemy units, I'll be using a while loop in sudo code it would look something like this.

public Transform; (Attach the prefab in the inspector)

Somefunction() {

int i = 0;
arraylist goes here;

while (i < 10) {
arraylist[i] = Newly instantiated object.


My object with the collider will then have something like the following:

OnTriggerEnter() {
get specific object somehow
add to arraylist

I can't use the tag function because all the objects will have the same tag, the only thing I assume will be different is the position of the object in the spawn array (ie: arraylist[1] arraylist[2] etc.)

If I created a public accessor method on the trigger object could I call this method to return a string name or something special that identifies the object uniquely compared to the rest of the objects in the scene or does Unity already do this somehow natively?


4 Answers 4


You could just add a script component that simply contains an int for an id with the start method containing some logic to initialize it to a unique value. Once you do that, you can use a hash table for storing and accessing the gameObjects with their ids.

  • \$\begingroup\$ Are you able to point me in the right direction for working with Hash Tables. I'll have a work on that tonight thanks :D \$\endgroup\$
    – Scott
    Commented Jan 31, 2013 at 22:54
  • \$\begingroup\$ If you are using C#, Google the Dictionary data structure. c-sharpcorner.com/UploadFile/mahesh/dictionary-in-C-Sharp This seems to be an alright source. You'll simply use your id int as the key and the gameObject reference (or whatever component you specifically need) as the value. \$\endgroup\$ Commented Feb 1, 2013 at 20:32
  • \$\begingroup\$ You could leverage GUID for obtaining a unique identifier msdn.microsoft.com/en-us/library/system.guid.newguid.aspx \$\endgroup\$ Commented Jul 18, 2014 at 12:36

First things first: do not use ArrayList. Use List<T>, for it will make your life simpler and nimbler.

How you keep track of these GameObjects depends on what you want to do with them. Hashtables and Dictionaries are a great way to associate objects with other data. (See the .Net documentation for the generic dictionary, nearby you'll also find List<T>'s docs).

But, if you want to get a reference to the GameObject, all Component subclasses (including Collider, Transform and any MonoBehaviour) all have a reference to the GameObject they're attached to by calling theComponentIdentifier.gameobject. This is probably what you want to store.


You could always change the name.

//make ten objects
    var theOb = Instantiate(myPrefab,transform.position,transform.rotation);
    theOb.name = "ob"+i;  //its name will include an index number
    theOb.parent = transform; //it will be a child object 

//modify the third object, finding it as a child

//modify the eighth object, finding it by a static GameObject method

(I'm having a hard time understanding your explanation of what you're trying to do. So I'm answering based on what I think you're trying to do, but my apologies if I misunderstood.)

I would use a Dictionary for this purpose. That data type is great for associating other data with specific objects. Also I would use List instead of ArrayList, so both the Dictionary and List would have types declared in <>. So like I would declare (c#):

public Dictionary<GameObject, List<GameObject>> objectMap;
void Start() {
  objectMap = new Dictionary<GameObject, List<GameObject>>();

then the collider script would look like:

void onTriggerEnter() {

  // in case there isn't a list for this object yet
  if (camera.objectMap[this.gameObject] == null) {
    camera.objectMap[this.gameObject] = new List<GameObject>();

  // mapping objects to lists happens here

In this way the Dictionary of objects maps different objects to different lists.

(Michael's answer also mentions Dictionaries, but isn't very specific about them)


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .