- Home /
Automatic Numbering (JS)
Ok, I want to have a quick script which will automatically number all the gameobjects.
So I have five of them, each called 'AutoPlatCenter':
AutoPlatCenter
AutoPlatCenter
AutoPlatCenter
AutoPlatCenter
AutoPlatCenter
What I want is the script to go through, find the all the objects with that name and add a number on each:
AutoPlatCenter1
AutoPlatCenter2
AutoPlatCenter3
AutoPlatCenter4
AutoPlatCenter5
The restrictions are:
Has to be on Awake ();
The script is attatched to each of the 'AutoPlatCenter' gameObjects.
Must be able to work for any amount of 'AutoPlatCenter' gameObjects.
I've done one which works, but I want to make it more effecient so it doesn't create null gameObjects and isn't based on which autoplat loads first but rather the order they are in:
function Awake () {
if ((gameObject.name).Contains("Auto")) {
var AutoNamerObj = new GameObject();
AutoNamerObj.name="PlatAutoNamer";
AutoNamerObj.tag="PlatAutoName";
gameObject.name=gameObject.name + (GameObject.FindGameObjectsWithTag("PlatAutoName").length);
}
Thanks
Answer by EvilTak · Jan 15, 2015 at 03:27 AM
You can use loops inside Awake. Code to do that is really simple (put it in Awake):
GameObject go = GameObject.Find(yourObjectsName);
for(int count = 1; go != null; count++) {
go.name += count.ToString();
go = GameObject.Find(yourObjectsName);
}
This will work for any number of objects. Make sure you replace yourObjectsName with your objects to number's names. Its C#, but I think you can easily translate it to UnityScript (just replace int and GameObject at declaration of count and go to var).
Here is the UnityScript version.
private var go : GameObject; // Reference to a GameObject
function Awake ()
{
// Find a gameobject with the specified name
go = GameObject.Find( "AutoPlatCenter" );
// Iterate, and check if the gameobject exist ...
for ( var i : int = 1; go != null; i++ )
{
// Set the gameobject's name
go.name += i.ToString();
// Find another gameobject with the specified name
go = GameObject.Find( "AutoPlatCenter" );
}
}
Thank you @ranso$$anonymous$$k! +1'd your comment! BTW it would be better if you define "go" GameObject in the Awake function since we don't need it later on... Can't edit comments with my karma, otherwise would have done it myself ;)
Answer by ThomasMarsh · Jan 15, 2015 at 12:39 AM
Instead of automatically numbering the variables, you could create an array of GameObjects. Then you could reference each using the syntax of arrayName[number]. This would be a much better solution in the long term. Here are some links for documentation and tutorials about arrays: http://docs.unity3d.com/ScriptReference/Array.html http://unity3d.com/learn/tutorials/modules/beginner/scripting/arrays
The JS array class should never be used, though. There are no end of questions on this site about problems caused by using it. http://wiki.unity3d.com/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use%3F
I know but that link also shows some of the syntax of arrays in normal use lower down the page.
Your answer
Follow this Question
Related Questions
How to reference GameObject in Awake or Start 2 Answers
static var for one object. 2 Answers
"GameObject.Find();" not working 2 Answers
Get game component type? 1 Answer
JavaScript 3 Arrays questions 1 Answer