Instantiate ID

Hello everyone,

I am creating several clones from a prefab.
At some time, I want to move these clones, with a specific way.

I’d like to know if it is possible to give something to these clones, such as an ID, to be able to call them later from a script.

For the moment, I am thinking about creating an array, and write something like:

Clones[ID_clone] = instantiate(Prefab)

Or maybe I will explore another solution:

Clone.GetComponent(MyScript).someVariable = ID_clone;

But I don’t know how to find the object with a script where variable = ID_clone.

Well, if anyone can give me advice, he is most welcome ^^

Well, if you are trying to reference a specific script on the clones, it'd be easiest to use an array of script instances. If you create all the clones at the same time, you can use

var cloneScripts : MyScript[] = MyScript[numberOfClones];

for(var i : int = 0; i < cloneScripts.Length; ++i)
{
    cloneScripts *= Instantiate(Prefab).GetComponent.<MyScript>();*
*}*
*```*
*<p>Then, you can just reference them by index.</p>*
*<p>You can also give the clones actual ID numbers in this way- just use the line</p>*
*```*
_cloneScripts*.id = i;*_
_*```*_
_*<p>after you instantiate them (assuming, of course, that the MyScript class includes an integer variable called 'id').</p>*_
_*<p>If you are not creating the clones at the same time, you can use this-</p>*_
_*```*_
_*var cloneScripts : System.Collections.Generic.List.<MyScript> = new System.Collections.Generic.List.<MyScript>();*_
_*```*_
_*<p>Then, whenever you spawn a clone use</p>*_
_*```*_
_*cloneScrips.Add(Instantiate(Prefab).GetComponent.<MyScript>());*_
_*```*_
_*<p>Now, you have a list of clones that you can access with an integer index. You should keep a count in your controller script of the 'next available id' so that you know what you are up to. Otherwise, you can use cloneScripts.Count to find out how many you have at any given time.</p>*_

The first alternative is the best when you have the ID and want the object. If you have the object and want its ID, the second alternative is better. I once needed both, and used a mix of these two approaches: defined consecutive IDs to the instantiated objects in Awake, created the array and stored the object transforms in the [ID] element in Start - like this (object script):

static var curID: int = 0; // global variable with the currently available ID
static var allObjects: Transform[]; // global array of objects
var myID: int; // member variable with this object's ID

function Awake(){
  myID = curID++; // assign IDs when creating the objects
}

function Start(){
  if (allObjects.length==0){ // the first object to execute Start allocates
    allObjects = new Transform[curID]; // the actual array with the right size 
  }
  allObjects[myID] = transform; // each object stores itself in the array
}

This code is suitable for objects created at game start. If you want to create objects during the game, an Array or List should be used.