1
\$\begingroup\$

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?

\$\endgroup\$

4 Answers 4

2
\$\begingroup\$

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.

\$\endgroup\$
3
1
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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);
\$\endgroup\$
0
\$\begingroup\$

(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
  camera.objectMap[this.gameObject].Add(hitObject);
}

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)

\$\endgroup\$

You must log in to answer this question.

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