- Home /
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 ^^
Answer by aldonaletto · Jan 24, 2012 at 10:42 AM
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.
Thanks, I think that's what I need. I'll create my objects during the game, but I understand your logic.
Small question though, what command are you using to find the ID when you have the object ? And are you exploring all instantiated objects to find the one you want ?
I'm not sure I like the idea of using a static variable for the IDs. I'd much rather have the object in charge of instantiating them assign the IDs.
@Pierre: each object has a myID variable assigned at Awake. If I click the object, for instance, I can get its script and read myID to know its ID number.
@syclamoth: I used this to catalog some scene objects at Start - they were created in the Editor, thus there was no creator object (it was me...). If you instantiate the objects during the game, it's much better to have a creator object and let it manage things, like you suggested.
Well finally I ended with a simple solution in which I am changing the name of my instantiated object to "objectname"+id.
Answer by syclamoth · Jan 24, 2012 at 09:53 AM
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[i] = Instantiate(Prefab).GetComponent.<MyScript>(); }
Then, you can just reference them by index.
You can also give the clones actual ID numbers in this way- just use the line
cloneScripts[i].id = i;
after you instantiate them (assuming, of course, that the MyScript class includes an integer variable called 'id').
If you are not creating the clones at the same time, you can use this-
var cloneScripts : System.Collections.Generic.List.<MyScript> = new System.Collections.Generic.List.<MyScript>();
Then, whenever you spawn a clone use
cloneScrips.Add(Instantiate(Prefab).GetComponent.<MyScript>());
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.
I am not creating the clones at the same time. Could you be a bit more explicit on your solution ? This is a bit unclear for me.
Great thanks.
Well, I'm afraid I don't quite understand the specifics of what you will be using the 'id numbers' for. The details really depend on exactly what they're supposed to be able to do.
Well, this is a server / client boardgame game with a fully authoritative server. On the server, I create a token which represents a player. When, on the client, the player gives a move order, I want to transfer the order to the token on the server. Therefore, I'd like to give to the token the player id, so that when I receive a move order from a player, I can identify the token to move.
I hope I am clear ^^
I see. Are you using inbuilt networking? The NetworkView kind of provides all of this functionality.