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 plzburnthis · May 19, 2014 at 05:41 PM · c#findgameobjectswithtagcountlength

How to use FindGameObjectWithTag to get 0

I've been trying to use a simple script to count the number of GameObjects to print to a GUI. I can't figure out how to properly do it. My difficulty is that Length always returns 1. How should I change this code?

     public static int count = 0;
     private GameObject[] getCount;
 
 void Update () {
         getCount = GameObject.FindGameObjectsWithTag("BallsTag");
         count = getCount.Length;
 }
Comment
Add comment · Show 2
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 robertbu · May 19, 2014 at 05:48 PM 2
Share

I don't see anything wrong with this code. Are you sure there are no game object with the tag 'BallsTag' in the scene? Perhaps you have a ball in the scene you use to Instantiate() from?

avatar image fafase · May 19, 2014 at 06:12 PM 2
Share

You could print the name of the object and its position, that would tell what and where it is.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Wiki

Answer by plzburnthis · May 20, 2014 at 03:26 PM

////////Edit: I missed how I fixed my problem, the text below is not the solution/////////

Thank you to both of you for replying to my unclear question. The answer turned out to be that because I was using this script to destroy object itself:

     void OnBecameInvisible()
 {
         DestroyObject(gameObject);
         }

The final object was destroyed/removed from hierarchy but not completely disappearing. I fixed my problem by changing the code to:

      void OnBecameInvisible()
 {
          DestroyObject(this.gameObject);
             }
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 fafase · May 20, 2014 at 04:03 PM 0
Share

Actually, both code are the same. Your fix is from somewhere.

The this keyword is just a pointer to the actual object calling the method, in your case, the script on which it is called. The script inherits gameobject reference from $$anonymous$$onoBehaviour, so using this.gameObject or simply gameObject result in the same.

avatar image plzburnthis · May 20, 2014 at 05:01 PM 0
Share

@fafase, thank you for your clarification! =D

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

20 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Count the contents of the list from FindGameObjectsWithTag 1 Answer

Detect clone using FindGameObjectsWithTag method. 1 Answer

GameObject.FindGameObjectsWithTag still finding destroyed object (C#) 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