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 /
This question was closed May 28, 2019 at 10:23 PM by Bunny83 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Divinum_Laminas · May 20, 2016 at 03:50 AM · arrayboolean

How do I check if all booleans in an array are true?

So, basically what I'm trying to do is write a bit of code that checks if all my objectives are complete, and if so, load a scene that says you've won.

My current mission checker is working fine, so when kill all enemies, mission A being complete returns true, but what I want to do is check if every mission is complete, so that's A, B, C, etc, all returning true, and then doing something when that happens.

This is the code that checks if each mission's requirements are met, and returns true if so. I just don't know where to go next..

 using UnityEngine;
 using System.Collections;
 
 public class Objectives : MonoBehaviour 
 {
     public gameObjectives[] MissionList;
 
     int objectiveDone = 0;
     void Update()
     {
         //Debug.Log (MissionList.Length);
         for (int i = 0; i < MissionList.Length; i++) 
         {
             int missionsDone = 0;
 
             for (int j = 0; j < MissionList[i].objectiveObject.GetComponentsInChildren<EnemyBuilding>().Length; j++)
             {
                 if(MissionList[i].objectiveObject.GetComponentsInChildren<EnemyBuilding>()[j].done)
                 {
                     missionsDone++;
                 }
 
                 if(missionsDone == MissionList[i].objectiveObject.GetComponentsInChildren<EnemyBuilding>().Length)
                 {
                     //Debug.Log("FINISHED OBJECTIVE " + i);
 
                     MissionList[i].isComplete = true;
                 }
             }
         }
     }
 }
Comment
Comments Locked · Show 1
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 Divinum_Laminas · May 20, 2016 at 12:50 AM 0
Share

Edit: I have made it work using a very clunky method.

 if ($$anonymous$$issionList[1].isComplete == true && $$anonymous$$issionList[2].isComplete == true && $$anonymous$$issionList[3].isComplete == true && $$anonymous$$issionList[4].isComplete == true) 
                 {
                     Application.LoadLevel("winScene");
                 }

Is there a neater way of doing this?

2 Replies

  • Sort: 
avatar image
15
Best Answer

Answer by daleth90 · May 20, 2016 at 05:32 AM

 private bool IsAllMissionComplete() {
     for ( int i = 0; i < MissionList.Length; ++i ) {
         if ( MissionList[ i ].isComplete == false ) {
           return false;
         }
     }
 
     return true;
 }

I think you know how to use this function. :)

Comment
Comments Locked · 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 Masterio · May 20, 2016 at 06:43 AM 0
Share

This is the most opitimal solution ;)

avatar image Divinum_Laminas · May 20, 2016 at 07:08 AM 0
Share

Bang! Worked a charm. Thank you very much!

avatar image
1

Answer by aditya · May 20, 2016 at 05:41 AM

 bool[] missionArray;
 bool allMissionsComplete = true;
 
 missionArray = new boll[3]{true, true, true};
 foreach(bool currentMission in missionArray){
     if(!currentMission){
         allMissionsComplete = false;
     }
 }
 
 if(allMissionsComplete){
     // ALL MISSIONS ARE COMPLETE
 }else{
     // NOT ALL MISSIONS ARE COMPLETE
 }
Comment
Comments Locked · Show 3 · 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 20, 2016 at 07:04 AM 0
Share

You should put a break after line 7 since it'S pointless to iterate through the remaining elements if one is false.

avatar image aditya Bunny83 · May 20, 2016 at 08:14 AM 0
Share

i thought of the same but dropped the idea thinking that it might not be a good idea to FORCE a loop to stop ... just asking, is it okay to ter$$anonymous$$ate loops if their objective is completed ?

avatar image Eno-Khaon aditya · May 20, 2016 at 09:03 AM 0
Share

Yep.

In this case, especially, once you've reached your objective state, you can be done. Since it's assumed that the boolean will be true until proven false, the instant it's been proven false is the moment you can bail out of the loop.

Follow this Question

Answers Answers and Comments

9 People are following this question.

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

Related Questions

question about array or list of boolean 1 Answer

Adding booleans to array and randomize 0 Answers

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

String comparison, can't use false result as string. 4 Answers

How can I do a foreach loop for an array of booleans? 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