Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Lemo76 · Jan 14, 2015 at 11:41 PM · javascriptgameobjectawakeautomatic

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image richyrich · Jan 15, 2015 at 01:40 AM 0
Share

Why do you want to add a number to each of them?

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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).

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ransomink · Jan 16, 2015 at 03:28 AM 1
Share

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" );
     }
 }
avatar image EvilTak · Jan 16, 2015 at 09:18 AM 0
Share

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 ;)

avatar image
0

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eric5h5 · Jan 15, 2015 at 01:55 AM 0
Share

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

avatar image ThomasMarsh · Jan 16, 2015 at 05:02 AM 0
Share

I know but that link also shows some of the syntax of arrays in normal use lower down the page.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges