- Home /
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;
}
}
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!
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
You guys, I now this was written ages ago. But it helped so much, even now. Thanks!
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.
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.
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
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
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. :(