- Home /
generating unique names for prefabs within an array
Hi there,
I'm currently having some trouble creating unique names for my prefabs.
I have instantiated all my prefabs within an array.
Below is the code that I have used to Instantiate my prefabs.
I'm trying to build a scene using different types of tiles.
So what your seeing are a combination of neutral tiles and tiles that have arrows on them.
for (var j: int = 0; j < tileArrayList[i].Count; j++){
if (tileArrayList[i][j] == 0) {
//tile appears
//Debug.Log("Start instantiating tiles");
//newTile=Instantiate (tileEmpty,Vector3(tilePosX,0,tilePosZ), Quaternion(0,0,0,0));
//newTile.name="TileEmpty";
} else if (tileArrayList[i][j] == 1) {
//tile appears
newTile=Instantiate (tileNeutral,Vector3(tilePosX,0,tilePosZ), Quaternion(0,0,0,0));
newTile.name="TileNeutral";
} else if (tileArrayList[i][j] == 2 || tileArrayList[i][j] == 3 || tileArrayList[i][j] == 4 || tileArrayList[i][j] == 5 ) {
newTile=Instantiate (tileSingle,Vector3(tilePosX,0,tilePosZ), Quaternion(0,0,0,0));
if(tileArrayList[i][j] == 2){
newTile.name="TileSingleUp";
}else if(tileArrayList[i][j] == 3){
newTile.name="TileSingleDown";
newTile.transform.eulerAngles.y-=90;
}else if(tileArrayList[i][j] == 4){
newTile.name="TileSingleLeft";
newTile.transform.eulerAngles.y-=180;
}else if(tileArrayList[i][j] == 5){
newTile.name="TileSingleRight";
newTile.transform.eulerAngles.y-=270;
}
}
Below is how the tiles are layed out after previewing in the game mode
What will happen here is that our robot will walk on to the tile that is pointing to the right and the tile will move to the right.
Now the problem that we're encountering here is that if we walk on the next arrowed tile, the first arrowed tile will still continue to move and that isn't what I want.
There is a post here in answer unity3d that has a similar problem http://answers.unity3d.com/questions/8885/affecting-only-one-prefab-in-code-script
and the solution was to use something called GenerateUniquename();
but I can't seem to find any reference on this term.
Answer by Ray-Pendergraph · May 05, 2010 at 02:34 PM
As a general rule, you can always use System.Guid (MSDN documentation) and append your names with a unique value using the Guid's ToString() method. I use this a lot when I need to uniquely identify resources at run time. These will not be very human readable.
Let me clarify: newTile.name = "TileSingleRight_" + System.Guid.NewGuid().ToString();
They will definitely be unique. Of course if you wanted to somehow systematically reference them with the name, then $$anonymous$$olix's answer might be better.
@Ray, you can make the URL a link by using the world button. Also, it's not quite guaranteed, but close: "The chance that the value of the new Guid will be all zeros or equal to any other Guid is very low."
Answer by Molix · May 05, 2010 at 03:42 PM
I don't think the function mentioned in that other answer actually exists--I think it was just meant as an example. If you just want unique names, you could just append the index you're using, e.g.
newTile.name = "TileSingleRight_" + i + "_" + j;
It may even help when you're debugging.