Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
add comment

3 Answers

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.

share|improve this answer
    
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 –  Scott Jan 31 '13 at 22:54
    
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. –  KlashnikovKid Feb 1 '13 at 20:32
add comment

You could always change the name.

//make ten objects
for(i=1;i<=10;i++){
    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
transform.Find("ob3").Translate(Vector3.right);

//modify the eighth object, finding it by a static GameObject method
GameObject.Find("ob8").transform.Rotate(Vector3.up);
share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.