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
0
Question by yapaysinek · Jul 12, 2016 at 09:53 PM · unity 5arraybooleanintbool

Get the number of false bools in GameObject[] (array)

Hey Unity Pros,

So this has been my nightmare for the last couple of hours trying to get this working. Basically, I want to get the number of false booleans in an array of GameObjects and simply display it as an int value. I dont know if this thing is too complicated and beyond my coding skills(which is pretty low btw) or Im just too dumb for this sh*t lol.

Anyway, Id really appreciate it if someone could help me!

Thanks in advance.

UPDATE: I have added a sample script to roughly visualize what i was trying to explain above.

 void Update () {
 
         bulletCounter.text = "BULLETS: " + //plus the number of bullets that hasnt been fired yet

 // i tried with for loop but it gave me the name of the GameObject instead of the total number of not fired bullets
 
         for (int i = 1; i < projectiles.Length; i++) {
 
             if (projectiles [i].GetComponent<Projectile> ().hasBeenFired == false) {
 
                 bulletCounter.text = "BULLETS: " + projectiles[i].ToString();
                 return;
             }
         }
















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

Answer by Pzula · Jul 13, 2016 at 01:59 AM

Hi yapaysinek,

The best way to approach this is to have an integer which you increment every time you find a boolean which is false.

I noticed in your original code you had for (int i = 1; i < projectiles.Length; i++) {. If you want to loop through every element in the array you should set i = 0 as arrays start at 0, not 1.

See the below code for my solution to your problem:

     void Update () {
 
         // The number of false booleans in our array
         int numberOfFalseBooleans = 0;
 
         // Loop through every array element
         for (int i = 0; i < projectiles.Length; i++) {
             // We check each array element one at a time
             // We are checking if this projectile hasBeenFired is equal to false
             if (projectiles [i].GetComponent<Projectile> ().hasBeenFired == false) {
                 // If it is equal to false, add one to our numberOfFalseBooleans counter
                 numberOfFalseBooleans++;
             }
         }
 
         // Finally, display the number of false booleans
         bulletCounter.text = "BULLETS: " + numberOfFalseBooleans;
     }


Hope this helps!

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 yapaysinek · Jul 16, 2016 at 08:04 PM 0
Share

Hey Pzula,

thank you and all of you who replied to my question and tried to help me. The solution above you sent me works just fine! Thank you again.. you made my day brotha'!! :DDD

avatar image mirmarka · May 17, 2018 at 10:36 AM 0
Share

You guys, I now this was written ages ago. But it helped so much, even now. Thanks!

avatar image
1

Answer by Devastus · Jul 12, 2016 at 11:02 PM

There's a couple ways to do this. If I understood this right, one way would be to have an int variable that stores the projectile array's length and drop it's value accordingly:

 int projectileAmount = projectiles.Length;
 
 for (int i = 0; i < projectiles.Length; i++) {
    if (projectiles [i].GetComponent<Projectile>().hasBeenFired == true) {
       int projectileAmount -= 1;
    }
 }
 
 bulletCounter.text = "BULLETS: " + projectileAmount;

Note that I am modifying the text after the loop.

Now, I don't know your particular setup, but for this problem alone this should work well enough.

EDIT: I had forgot to edit the boolean value to true, sorry. What I am essentially doing here is checking how many bullets you have left after firing them and then printing that out. If that is not what you wanted, Pzula's answer has a correct way of checking the number of false booleans in your array which is what you were originally asking. Sorry for misreading there.

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 Pzula · Jul 13, 2016 at 02:06 AM 0
Share

Hey Devastus,

Your code here sets the projectile amount equal to the length of the array, and then subtracts one every time it finds a projectile with a false boolean value. This leaves you with a projectile amount of all the booleans which are true. I'm not sure that's what yapaysinek is after.

To correct this code you'd change the if statement to check if the hasBeenFired is true. This will subtract the number of true booleans from the projectileAmount, leaving you with the number of false booleans.

avatar image Devastus Pzula · Jul 13, 2016 at 12:40 PM 0
Share

You're right, I reckoned that it's what is wanted here from the code snippet though. $$anonymous$$y bad if I indeed understood wrong.

EDIT: Also whoops, I totally missed the part of not editing the boolean value to true. Thank you for pointing that out :P

avatar image
-1

Answer by Anas173 · Jul 12, 2016 at 10:10 PM

Hey! You can loop through your array length and access whatever you want!)

 For(int i = 1; i<YouArrayName.Length;i++)
 {
   // get ur false bool here

          if (YouArrayName[i])
          {
              return YouArrayName[i];
          }
      }
 }

This will give you the true booleans. So now you can now how many false or true you got

I

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 yapaysinek · Jul 12, 2016 at 10:50 PM 0
Share

Hey loue133,

thank you for your answer! I have tried using your method but couldnt get it to work. I have attached a sample script above with the for loop but ins$$anonymous$$d of the total number of bullets that hasnt been fired yet it gave me the name of the GameObject. :(

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

89 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I move back to Element 0 in an array to reset a loop? 1 Answer

bool changing on its own? 0 Answers

question about array or list of boolean 1 Answer

How can I do a foreach loop for an array of booleans? 1 Answer

bool[string] = true; array possible? 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