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 *.GetComponent<Projectile> ().hasBeenFired == false) {*
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 *.GetComponent<Projectile> ().hasBeenFired == false) {*
// If it is equal to false, add one to our numberOfFalseBooleans counter*
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 *.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.