Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by xyon81 · Jan 13, 2016 at 01:55 PM · c#unity 5

How do I assign ascending numbers to a group of the same objects

I'd like to create a numbered group of objects.

So if I had 15 of the same enemy, I'd like to number each in turn, so that one is called 1, the next is called 2, etc

I'm using it the latest version of unity 3d

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Morozov_5f · Jan 13, 2016 at 02:29 PM

It can be done in two ways:

  1. Use FindObjectsOfType() function

  2. Use static variable

First way is simple enough - in your enemy behavior (for example, let's call it EnemyBehavior) at Start() method you simply type this code:

 void Start()
 {
      /*
      ... here goes your initialization
      */
      gameObject.name = FindObjectsOfType<EnemyBehavior>().Length.ToString();
 }

How it works - it gets all your objects with type EnemyBehavor on scene and, gets their count and assigns it to game object's name. Note that this method is simple, but slow, if performance is very critical.

Second way is a bit more complicated. You should put a public static variable in your EnemyBehavior class, so you can access it from every object of that class and than, on Start and on Destroy you just add this counter 1 or -1:

 public static uint EnemiesCount = 0;
 
 void Start()
 {
     // Again other initialization
     gameObject.name = ++EnemyBehavior.EnemiesCount;
 }
 
 void OnDestroy()
 {
     --EnemyBehavior.EnemiesCount;
     // Other destroy stuff, if you have some
 }

This way is very fast and relative simple too, which to use - depends on you :) I'd rather use second way, because it's fast and doesn't use any additional memory.

Comment
Add comment · Show 3 · 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 Morozov_5f · Jan 13, 2016 at 02:32 PM 0
Share

You can find more information about this function here: FindObjectsOfType()

avatar image xyon81 · Jan 13, 2016 at 03:04 PM 0
Share

I noticed you called it as a string, does that matter. If i'm using a called variable from another action (1,2,3,4, etc) will it still match it to that enemy?

ie, can I say if gameObject.name = 1 - is it going to get confused by strings vs numbers?

The 2nd looks like it will work a bit better for what I have in $$anonymous$$d though :) thanks

avatar image Morozov_5f xyon81 · Jan 13, 2016 at 03:09 PM 1
Share

Yeah, it will matter. I have to correct myself, in second way you should assign name in that way:

 gameObject.name = (++EnemyBehavior.EnemiesCount).ToString();

Otherwise, if you don't explicitly do toString conversion, compiler will be confused by trying to assign number to string. To yeah, you have to do the conversion.

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

69 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 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 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

IsGrounded not updating properly 1 Answer

How to monetize Unity game ? 0 Answers

Forge-like in game editor 0 Answers

how can i control scale speed ? 1 Answer

Building system problem 0 Answers


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