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 Hamesh81 · May 15, 2013 at 01:50 PM · arraylistitemsallgeneric list

How can I check if ALL items in an array/list meet a condition?

I have a generic list (but this can apply to arrays also) which I check to see if any of the items have their renderer turned off, those that do simply get added to another list. I want to add in a condition in case none (not some) of the items have their renderers turned off i.e all of the items have their renderers turned on. I'm not sure how to do this. I've tried using an else statement, which when met, I would like to add only the first item in the current list to another list; but for some reason it always adds 2 of this item instead of one. Should I make a separate for loop just for this, or what should I do?

Comment
Add comment · Show 4
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 raimon.massanet · May 15, 2013 at 02:04 PM 0
Share

How many of allProjectiles have their render turned off?

avatar image Hamesh81 · May 15, 2013 at 02:39 PM 0
Share

In the beginning all of them have it off, but one by one each item gets its renderer turned on so eventually all of them will have it turned on. It's at this point that I simply want to add the first item in the list to the second list. Does that make sense?

avatar image raimon.massanet · May 15, 2013 at 02:44 PM 0
Share

I don't quite understand what you are trying to do, but maybe you are forgetting to clean the second list, or maybe you have more than one renderer turned off and that's why you are seeing two objects added.

avatar image Hamesh81 · May 15, 2013 at 03:17 PM 0
Share

Ok, I have several projectiles in the scene (I do not instantiate them, there is a finite number), all of which are referenced by the allProjectiles list on Start. By default all of these have their renderers turned off, and only when a projectile is needed (eg. a weapon is fired) I search the allProjectiles list for those with their renderers turned off, add them to a second list availableProjectiles (without removing them from the first), and then get the first item from availableProjectiles, turn on its renderer and fire it from the weapon.

So as you can see this will mean that eventually every projectile will have been fired i.e every projectile will have its renderer turned on. Therefore I need to be able to check for this condition and ins$$anonymous$$d of adding projectiles (from allProjectiles) with their renderer turned off, I will now add the first from this list ins$$anonymous$$d. The full script is available here in the first answer:

http://answers.unity3d.com/questions/451699/finding-the-first-gameobject-in-a-list-that-meets.html

2 Replies

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

Answer by Bunny83 · May 15, 2013 at 03:18 PM

This is simple boolean logic ;)

All you need is a boolean value that is initially true. Inside your loop you set this boolean to false whenever one of the elements does not meet the condition. If this variable is still true after the loop is done, all elements met the condition.

In your case something like that:

 var allReady = true;
 for (var avProjectile : GameObject in allProjectiles)
 {
     if (avProjectile.renderer.enabled)
     {
         allReady = false;
     }
 }
 if (allReady)
 {
     // All renderers are off
 }
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 Hamesh81 · May 15, 2013 at 03:27 PM 0
Share

Yes, this looks like exactly what I was asking for. Please give me a $$anonymous$$ute while I test this (or ten :) ... ...

avatar image Hamesh81 · May 15, 2013 at 04:36 PM 0
Share

... hmmm, it seems to be working ok so far. I'll need to do more testing and tweak things a bit more to get everything working completely, but I think this was the missing piece to the puzzle. Thanks a lot for your help Bunny83

avatar image
0

Answer by Aeron0 · May 15, 2013 at 02:50 PM

 using System.Collections.Generic;
 
 // Generic List array variables to hold your data
 List<avProjectile> allProjectiles;
 List<avProjectile> noRender;
 
 // These variables need instantiated
 allProjectiles = new List<avProjectile>();
 noRender = new List<avProjectile>();
 
 // Add your data in the list
 allProjectiles.Add(your avProjectiles);
 
 
 if (Input.GetKeyDown ("space")) 
 {
   int index = 0;
   
   foreach(avProjectile projectile in allProjectiles)
   {
     if(allProjectiles[index].renderer.enabled == false)
     {
       // Add the index projectile in the new list 
       noRender.Add(allProjectiles[index]);
 
       // remove that projectile from the old list
       allProjectiles.RemoveAt(index);
      }
      index++;
    } 
 
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 Bunny83 · May 15, 2013 at 03:25 PM 0
Share

Some points:

  • First of all he's using UnityScript, not C#. That's not a problem, but it seems you misunderstood the problem.

  • He's using a list / array of GameObject. avProjectile is just the name of the for-each loop variable.

  • using a foreach loop and additionally keeping an index doesn't make much sense.

  • changing a collection while you iterate over it with foreach will throw an exception.

  • An answer should at least explain a little bit what you're actually doing.

avatar image Hamesh81 · May 15, 2013 at 04:33 PM 0
Share

Thanks for your help Aeron0, but unfortunately this didn't solve my problem. Appreciate your effort though

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

17 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

Related Questions

A node in a childnode? 1 Answer

Emptying a Generic List / Unexpected Behaviour 1 Answer

identify and remove an item from a list 1 Answer

how add values to Generic.list ? 2 Answers

Generic List.Count always gives 0 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