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 ChickenReborn · Apr 09, 2012 at 09:09 PM · javascriptmathcount

A counting script?

Greetings Unity friends! I have a scripting question for you today:

Is there a script that can count or number how many of a certain object there are in a scene?

Specifically, I'm working on a game where there are a certain number of goals in each scene. I want my script to count how many goal objects are in the scene, compare that to how many goals that the player has made and, when all goals have been accomplished, register a win.

I looked up "count" in the scripting reference and like a hundred different things came up and none of them seemed to match.

Any help would be greatly appreciated, even if someone could point me in the right direction.

Thanks!

Dwayne The Chicken Reborn

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
2

Answer by gregzo · Apr 09, 2012 at 09:50 PM

Tag your goal objects as "Goal", then:

 var goals : GameObjects[] = GameObject.FindGameObjectsWithTag("Goal");
 var goalCount : int = goals.length;

As you can see here, FindGameObjectsWithTag returns an array of GameObjects. Then, the length of the array is your number of goals...

Comment
Add comment · Show 5 · 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 ChickenReborn · Apr 10, 2012 at 07:58 AM 0
Share

AWESO$$anonymous$$E! That helps a lot! Thanks!

avatar image gregzo · Apr 10, 2012 at 08:24 AM 1
Share

Glad it did! Btw, you should comment and not reply in these cases. Accepting my answer would also help others to identify it as such!

avatar image ChickenReborn · Apr 11, 2012 at 11:54 PM 0
Share

Yeah, I know, I just hit SEND before realizing it was a new answer box.

avatar image kardiona · Sep 30, 2014 at 10:39 PM 0
Share

Hello, I tried this script, but somehow got this error ins$$anonymous$$d

Assets/Script/PlayerController.cs(96,33): error CS1525: Unexpected symbol :', expecting )', ,', ;', [', or ='

Any assistance? Thanks!

avatar image Kiwasi · Sep 30, 2014 at 10:56 PM 1
Share

The script is in JavaScript, you are using C#. The appropriate C# script is as follows

 GameObjects[] goals = GameObject.FindGameObjectsWithTag("Goal");
 int goalCount = goals.length;

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

On Button press Move Value toward 2 Answers

How do I compare two binary values in Javascript? 1 Answer

Maths with variables 2 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