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 ^^
2 Answers
2
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.
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.
– Pierre_1Well, 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.
– syclamothWell, 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 ^^
– Pierre_1I see. Are you using inbuilt networking? The NetworkView kind of provides all of this functionality.
– syclamoth