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
1
Question by vittu1994 · Sep 28, 2015 at 06:38 AM · gameobjectlistelements

how to know when list is empty?

i wanna know when my list has no elements in it. I will add gameObjects to the gameObject list during runtime and i have an if statement that will check current scene index and i also wanna check when this certain list is empty? How should i do?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
13

Answer by GiyomuGames · Sep 28, 2015 at 08:28 AM

If your list is called "myList", then "myList.Count" tells you the number of elements in the list. Therefore "myList.Count == 0" will be true only when your list is empty.

Comment
Add comment · 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
3

Answer by MadDevil · Sep 28, 2015 at 07:33 AM

Use this :

if(random.Count.Equals(0)) print("List is empty");

Comment
Add comment · Show 1 · 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 MikeNewall · Sep 28, 2015 at 07:44 AM 3
Share

As long as you removed the objects from the list when you destroy them then the count will reduce. Otherwise the item will remain in the list and be null.

You can remove null items first with

 $$anonymous$$yList.RemoveAll(item => item == null);
avatar image
2

Answer by Satsujinken508 · Nov 26, 2016 at 04:37 PM

list.Any()

https://msdn.microsoft.com/en-us/library/bb337697(v=vs.110).aspx

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 MattDownie · Nov 28, 2016 at 12:07 PM -3
Share

This is the efficient answer - 'Count' is potentially slow. (If your list is a million items long, then Count will count every single entry, one by one, while 'Any' will just check the first item and then report back immediately.)

And yet... 'Any' doesn't compile for me in Unity:

Type 'System.Collections.Generic.List' does not contain a definition for `Any' and no extension method 'Any' of type 'System.Collections.Generic.List' could be found (are you missing a using directive or an assembly reference?)

So is there a 'using' directive that will make this work?

avatar image Satsujinken508 MattDownie · Nov 28, 2016 at 12:23 PM 1
Share

using System.Linq;

Also, if you don't know which directive it is, you can just find it in msdn aswell)

avatar image Johan-Ed MattDownie · Jun 04, 2018 at 12:22 PM 3
Share

A note to "'Count' is potentially slow.":

.

In the description of Count (https://msdn.microsoft.com/en-us/library/27b47ht3(v=vs.110).aspx) it is stated that: "Retrieving the value of this property is an O(1) operation."

.

In other words, finding the number of elements in a list does not require counting the list's elements. The number of elements in the list is kept in the list class and is incremented or decremented as you add or remove elements, then when you use Count this number is simply retrieved. Because of this Count is equally fast regardless of the number of elements in your list.

.

To summarize: count is likely the most efficient way to check if a list is empty (even if the list you check has millions of elements).

avatar image GiyomuGames · Nov 28, 2016 at 12:58 PM 1
Share

That's definitely a clean way to check that the list is empty. I will just point out that a Field Engineer in Unity explained in a conference about optimization that one should never ever use Linq at runtime because it is super slow. It's just FYI, for simple games it will probably be unnoticeable!

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

How to remove objects from a list ? 3 Answers

Can't add GameObjects to ArrayList 1 Answer

Keep adding targets to a list 2 Answers

Get first gameobject in a list and cycle through on keypress 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