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
1
Question by Johan 4 · Mar 02, 2011 at 05:50 PM · looptag

Loop through all objects with same tag?

How can I loop and then count all objects that share the same tag? Thanks.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Eric5h5 · Mar 02, 2011 at 06:39 PM

var objects = GameObject.FindGameObjectsWithTag("blah");
var objectCount = objects.Length;
foreach (var obj in objects) {
    // whatever
}
Comment
Add comment · Show 7 · 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 Jessy · Mar 02, 2011 at 06:41 PM 0
Share

Why objectCount?

avatar image Eric5h5 · Mar 02, 2011 at 06:55 PM 0
Share

Well, he wanted a count of all objects with a given tag, apparently.

avatar image Johan 4 · Mar 02, 2011 at 07:38 PM 0
Share

Can someone translate this to C#? I am getting problems doing that.

avatar image Eric5h5 · Mar 02, 2011 at 07:41 PM 1
Share

@Johan: That is C#. You requested C#, so that's what I posted, and it's working code.

avatar image Eric5h5 · Mar 03, 2011 at 12:55 AM 0
Share

@Johan: no, it is not Javascript. Javascript does not have the foreach keyword, and could use objects.length ins$$anonymous$$d of objects.Length. I guarantee 100% that's working and valid C#, since like most code I post, I actually test it to make sure I'm not writing nonsense.

avatar image Jabes22 Eric5h5 · Jan 19, 2017 at 06:33 AM -1
Share

It's not c# c uses int a not var keyword to instantiate variables.

avatar image ShayneK Jabes22 · Feb 24, 2017 at 02:50 AM 0
Share

It is in fact c# - see https://msdn.microsoft.com/en-us/library/bb383973.aspx

 var objectCount = objects.Length; //implicitly typed to int
 int objectCount = objects.Length; //explicitly typed to int
avatar image
2

Answer by FrHaYwOrKs · Apr 19, 2011 at 08:56 AM

While Eric5h5's answer is actually working for C#, I'm just posting the JS version, for all of you, lazy people! :)

var objects = GameObject.FindGameObjectsWithTag("blah");
var objectCount = objects.length;
for (var obj : GameObject in objects) {
   // whatever
}
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 cico · Nov 08, 2011 at 01:26 PM 0
Share

ahah love ya :)

edit: ups, this time I was writing C# :-\

avatar image isteffy · May 30, 2016 at 06:49 AM 0
Share

Eric5h5's answer is now JS in case anyone was like 'WHHAAAA?"

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

OnTriggerEnter only if two tags are found? 1 Answer

Not getting proper reference using Tags 1 Answer

Destroying gameObject destroys all objects 2 Answers

I am making a fishing game but i can't solve that fishies will die 2 Answers

Enable/Disable all children of parent with tag 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