void cube() {
var cubed = GameObject.CreatePrimitive(PrimitiveType.Cube);
cubed.name = "Cubes" + GameObject.FindGameObjectsWithTag("Cubes").Length.ToString();
}
Hi this function of mine, if I run it multiple times in another function it seems to name some cubes the same name. Is there another way to get unique name for cubes.
The names are really only for you to better organize the objects when looking at them in the editor (visually), which is irrelevant when you’re instantiating the cubes in a script. There are numerous (and far better) ways of accessing the GameObjects that you need rather than using unique names (like drag-and-drop into GameObject references in the inspector, or use FindGameObjectWithTag(s) on specially tagged objects and caching the references for future use in a variable or list.
If your cubes have some sort of special place in your gameplay that requires them to have a “name” value to display on the screen, you would be better off making a script to attach to them that holds that string value inside, rather than using the name of the object.
Keep your own counter and append that to the name instead of relying on a find call. It will be quicker and more reliable.
1 Like