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 /
avatar image
0
Question by gargd · May 18, 2018 at 09:53 AM · unityeditortagcounter

How can I keep track of gameobjects getting a particular tag?

I give multiple gameobjects same tag at different points. Everytime a gameobject gets that tag, I increment the counter. Let's say, there are 5 gameobjects in the scene with the same tag and the counter is incremented to 5. Now, I need to make the first gameobject to get that tag, which incremented the counter to1 , to do something, how would I go about it? I think it is a synchronisation problem, any fixes for this?

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
0

Answer by Temseii · May 18, 2018 at 10:49 AM

Incrementing that counter will only tell you how many gameobjects you've given the tag to, you can't actually really do anything with the number. I'm not sure I understand what you're trying to do here, but what you should probably do is use something like a Generic List to hold the actual gameobjects. Here's some pseudo code that might help you out.

          // Generic list with type gameobject
          List<GameObject> listOfGameObjects = new List<GameObject>();
  
          // Give the gameobject a tag and add it to the list 
          private void AddGameObjectToList(GameObject myGameObject) {
             myGameObject.tag = "YourTagName";
             listOfGameObjects.Add(myGameObject);
          }
  
          // Doing something with the gameobjects in your list (not a proper method of course, let's just use something as an example)
          private void DisplayGameObjects() {
  
              // Set every gameobject in your list to be active
              for (int i = 0; i < listOfGameObjects.Count; i++) {
                  listOfGameObjects[i].SetActive(true);
              }
  
              // Or you could just set the first gameobject in your list to be active
              listOfGameObjects[0].SetActive(true);
          }
 
 
Comment
Add comment · Show 9 · 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 gargd · May 18, 2018 at 05:20 PM 0
Share

Thanks a lot for your reply. Can you please confirm that when a gameobject in the list is destroyed, the list is automatically updated, right?

avatar image Temseii gargd · May 18, 2018 at 05:35 PM 0
Share

No, you have to separately remove the gameobject from the list, but that's easy. Just do listOfGameObjects.Remove(GameObjectName); where you're destroying the gameobject.

Don't forget to mark the answer if you got what you needed. :)

avatar image gargd · May 18, 2018 at 06:13 PM 0
Share

What if I change the tag of the gameobject which is within the list after it does what it is suppose to do, will that automatically remove that gameobject from the list? or is it once a gameobject is added in a list, it just stays there unless you explicitly remove it using: listOfGameObjects.Remove(GameObjectName);

avatar image Temseii gargd · May 18, 2018 at 06:16 PM 0
Share

Shouldn't make a difference, but feel free to try it out on your own. Debugging and trial & error is key.

avatar image gargd · May 18, 2018 at 06:25 PM 0
Share

Thanks a lot for your prompt replies and how can I make the first gameobject in the list active and all other gameobjects in the list inactive ? I tried : listOfGameObjects[0].SetActive(true); doesn't seem to be working. I just want the first gameobject in the list to do a thing, rest all should do nothing.

avatar image Temseii gargd · May 18, 2018 at 06:27 PM 0
Share

Show me the full code you're running please

avatar image gargd · May 18, 2018 at 07:35 PM 0
Share

I am stuck at the last part, the method you suggested is working but if I setactive just the first gameobject in the list as true and others false, the true one does what it is suppose to while the false ones disappears. I want that the false ones should wait until the second gameobject in the list gets to number 1 on the list and so on.

Hope you get what I mean..

avatar image Temseii gargd · May 18, 2018 at 07:48 PM 0
Share

You'll need to make several modifications to the last method if you want it to do anything useful. I'm going to need to see your code to know what exactly is going wrong there. If you could upload the whole class to pastebin and just copy paste the link here I'll have a look.

avatar image gargd Temseii · May 19, 2018 at 08:58 AM 0
Share

Thanks a lot for the pointers. I modified the code and made it work:)

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

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

Only tagged objects raise counter 1 Answer

Randomly changing between two tags using coroutine 2 Answers

Raycast object tag check? 3 Answers

CharacterController high on coffee: Bounces down slopes 0 Answers

Removing and Adding gameobjects to an array upon collision 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