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 Anxo · Sep 06, 2010 at 02:09 PM · arraylistcount

Array to create a list of objects in the scene?

Hi Unity Community.

I would like to make a list of objects in a scene. Lets say I design 3 space levels and I have a perfab called "commet". In level 1, I add 10 commets, in lvl two, 3 commets. And in level three, 12 comments.

Now I tell the player to destroy all comments in the scene and I want to display it like "1/10". I know there is a way I can create an Array tell the array to add all commets in the scene to it and and count them. I am just having a hard time finding a code snip of something similar to point me in the right direction. I am using Javascript in unity.

Thank you for your time.

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
3
Best Answer

Answer by · Sep 06, 2010 at 02:24 PM

You could just have a static variable that increments/decrements every time you destroy a comet. It seems as though you know how many comets there are in total at any given time. When you spawn more, you'd just add that same number to the current total.

static var cometsDestroyed : int = 0;
static var cometsTotal : int = 0;

When you spawn comets:

cometsTotal += numberOfCometsYouJustSpawned;

When you destroy a comet:

cometsDestroyed += 1;

Your GUI script could then just display cometsDestroyed/cometsTotal.


However, since you asked for a function that creates an array and counts them, this function will count how many GameObjects with the 'Comet' tag are still existing. The GameObject.Find searches aren't cheap, so you probably wouldn't want to do this every frame.

function CountComets () : int;
{
    var comets : GameObject[] = GameObject.FindGameObjectsWithTag("Comet");
    return comets.Length;
}

Honestly, I would suggest the first approach, unless there's a reason you need the function (that you didn't mention in your question).

Hope this helps.

Comment
Add comment · Show 4 · 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 Anxo · Sep 06, 2010 at 04:03 PM 0
Share

Ah thats great, thank you for your answer. I am not actually working with comets and such, I was just using it as an example so that I can ask my question without confusion.

I can just call that CountComet function from inside the Awake function so it wont do it every frame. I am looking to create around 50 levels or more in which you have to collect a particular item that i will place all over, but not all levels will have the same amount of items, and I have on master game controller code that controlls all the levels the same way. so I did not want to create a . " if application.loaded =

avatar image Anxo · Sep 06, 2010 at 04:05 PM 0
Share

to level 1 then item count = 10 and so on. I wanted a code that is relative to the level designed.

Again, Thank you for your answer, not tested yet but it looks like that will get me on the right track.

avatar image Anxo · Sep 06, 2010 at 04:32 PM 0
Share

worked perfectly, Here it is implemented.

function NuttCounter() : int { var Nutts : GameObject[] = GameObject.FindGameObjectsWithTag("nutt"); print(Nutts.length); TotalNutts = Nutts.length;

}

avatar image Anxo · Sep 06, 2010 at 04:34 PM 0
Share

then in the update function I have " if (NuttsCollected >= TotalNutts){ YouWin();}

so this will eli$$anonymous$$ate I wold say 40% of the work when I create the levels for this game, allowing me to create more levels without getting bored!

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

No one has followed this question yet.

Related Questions

bounds checking a list? 2 Answers

Get size of a vector3 array c# 1 Answer

Generic List.Count always gives 0 2 Answers

A node in a childnode? 1 Answer

Selection list from Array Unity - Random - GameObjects array 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