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 JoniBerg · Feb 02, 2019 at 11:16 AM · arraygameobjectsbooleans

how to check all elements in array

How to check one by one element is true in array. I collected part of toys and i have fully repaired toy.

I want check part of toys is false and one by one bool is true of 1/3.

And if all 3/3 is true fullyrepaired is active.

  for(int i = 0; i < buttons.Length; i++)
          {
              if(buttons[i].activeSelf == false)
              {
                  Debug.Log("ready");
                  break;
              }
          }


^^ My examble code from internet.

     for (int i = 0; i < isRepaired.Length; i++)
         {
             if (partOfToys[i].activeSelf)
             {           
                
               
                 isRepaired[index] = true;
                 print(i);
 
                
             }
 
             index++;
         }
 
         repairedIsReady(); // Check if all is true and active full Toy :) << This working right. 
 
 
     }

My own code with error. I collected 1/3 toy all 3/3 is true. WHY???

Can anyone help me?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by RadonRaph · Feb 02, 2019 at 11:54 AM

Hello, why you duplicate your index ? Var index and i does the same thing !

      for (int i = 0; i < isRepaired.Length; i++)
          {
              if (partOfToys[i].activeSelf)
              {           
                 
                
                  isRepaired[index] = true;
                  print(i);
  
                 
              }
  
              index++;
          }
  
          repairedIsReady(); // Check if all is true and active full Toy :) << This working right. 
  
  
      }

Try this ;) Raph

Comment
Add comment · 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 JoniBerg · Feb 02, 2019 at 12:02 PM 0
Share

Sorry i duplicate what?

Now code working. if one partOfToys = false ALL isRepaired bools = true.

Is so small error in code. :(

 for (int i = 0; i < isRepaired.Length;)
             {
                 //if (isRepaired[i] == false)
                 if (partOfToys[i].activeSelf)
                 {
 
 
                     isRepaired[i] = true;
 
 
 
                 }
 
                 i++;
             }
 
             repairedIsReady(); // Check if all is true and active full Toy :) << This working right. 
 
 
 
avatar image RadonRaph JoniBerg · Feb 02, 2019 at 02:09 PM 0
Share

Can you give us more ? we can't see what are isRepaired and partOfToys

avatar image JoniBerg RadonRaph · Feb 02, 2019 at 02:24 PM 0
Share
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System.Linq;
 
 public class ToyRepair$$anonymous$$anager : $$anonymous$$onoBehaviour
 {
 
     ToyRepair tR;
     public GameObject AllmoustRepairedCube;
     private GameObject repairedCube;
     public GameObject readytoy;
     public List<GameObject> partOfToys;
     public Transform[] spawnPoints;
     public bool[] spotUsed;
     public bool[] isRepaired;
 
 
     public void UseToyPart(GameObject toy)
     {
 
         var toys = Random.Range(0, partOfToys.Count);
 
        
             for (int i = 0; i < isRepaired.Length;)
             {
                 //if (isRepaired[i] == false)
                 if (partOfToys[i].activeSelf)
                 {
 
 
                     isRepaired[i] = true;
 
 
 
                 }
 
                 i++;
             }
 
             repairedIsReady(); // Check if all is true and active full Toy :) << This working right. 
 
 
         
     }
 
     void Awake()
     {
 
         for (int E = 0; E < spawnPoints.Length; E++)
         {
             Spawn();
         }
     }     
 
     void Spawn()
     {
         int bools = Random.Range(0, spotUsed.Length);
         int spawnPointIndex = Random.Range(0, spawnPoints.Length);
         int spawnObjectsIndex = Random.Range(0, partOfToys.Count);
        
 
         while ((spotUsed[spawnPointIndex] == true)) 
         {
             spawnPointIndex = Random.Range(0, spawnPoints.Length);
             
         }
 
         var toys =  Instantiate(partOfToys[spawnObjectsIndex], spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
        // partOfToys.RemoveAt(spawnObjectsIndex);
         spotUsed[spawnPointIndex] = true;        
         
     }
 
    public void repairedIsReady() 
    {
         bool allRepaired = true;
         foreach (var t in isRepaired)
         {
             if (!t)
             {
                
                 allRepaired = false;
                
                
                 break;                
             }
 
             //  allRepaired = isRepaired.Any(x => x); Tarkoittaa samaa kuin yllä oleva.
         }
 
         if (allRepaired)
         {
             readytoy.SetActive(true);
 
         }
 
         
 
     }
 }
avatar image
0

Answer by dan_wipf · Feb 02, 2019 at 02:53 PM

for checking all booleans in an array use linq: using System.Linq;

 if (myarray.All(x => x)){
 //do something }
Comment
Add comment · Show 10 · 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 JoniBerg · Feb 02, 2019 at 03:02 PM 0
Share

Yeah this Linq working on repairedIsReady() method.

But what working checking for all booleans for one by one?

avatar image dan_wipf JoniBerg · Feb 02, 2019 at 06:57 PM 0
Share

then best practice is a forloop, or you check if any(linqish) way if any part isnt repaired

avatar image JoniBerg · Feb 02, 2019 at 07:41 PM 0
Share
    public void UseToyPart(GameObject toy)
     {
 
         var toys = Random.Range(0, partOfToys.Count);
 
        
             for (int i = 0; i < isRepaired.Length;)
             {
                 //if (isRepaired[i] == false)
                 if (partOfToys[i].activeSelf)
                 {
 
 
                     isRepaired[i] = true;
 
 
 
                 }
 
                 i++;
             }
 
             repairedIsReady(); // Check if all is true and active full Toy :) << This working right. 
 
 
         
     }
 

What i say in if Statement? @dan_wipf I want set one bool true if i collected one part of toys.

Only if i collected all parts repairedIsReady(): method happend.

Now it hapend if i collected first part.

avatar image dan_wipf JoniBerg · Feb 02, 2019 at 09:00 PM 0
Share

well i guess now that the list of bool is dynamicly and changes it size, right? well then it should be like if part 13/13 is collected and bool 13=true (for example: myboolarray[mypartarray.length-1] == true) then should happen something

avatar image JoniBerg dan_wipf · Feb 02, 2019 at 09:04 PM 0
Share

you working with array in foreach loop? this index [mypartarray.length-1] take only last index in array.

avatar image JoniBerg · Feb 02, 2019 at 08:00 PM 0
Share

This is so simple problem how i set one bool true if one part is collected?

This is infuriating. :D

avatar image JoniBerg JoniBerg · Feb 02, 2019 at 09:46 PM 0
Share

Right now code is:

  public void UseToyPart(GameObject toy)
     {
         int isCollected = 0;
 
         while (isRepaired.Length > isCollected)
         {
 
             isRepaired[Random.Range(isCollected, isRepaired.Length -1)] = true;
             isCollected++;
             print(isCollected);
         }     
         
             repairedIsReady(); // Check if all is true and active full Toy :) << This working right. 
         
     }


but still not working like I want :(

avatar image dan_wipf JoniBerg · Feb 03, 2019 at 12:59 PM 0
Share

Well you Random.Range Code won't work like you expect. because it likely will not set all bools to true. repairedIsReady although will be true always because if int isCollected> isRpeaired.Length it will cast.

so what I'd do is following:

 public void UseToyPart(GameObject toy)
      {
          int isCollected = 0;
  
          if(isRepaired.Length-1 < isCollected)
          {
  
              isRepaired[isCollected] = true;
              isCollected++;
              print(isCollected);
          }else{     
          
              repairedIsReady(); // Check if all is true and active full Toy :) << This working right. 
 }
          
      }
 
avatar image JoniBerg · Feb 03, 2019 at 04:15 PM 0
Share

I try your code @dan_wipf.
but why iscollected not more +1? only 0 >> 1 but not 1 >>2 and 2>>3

see this link photo.

unity1.jpg (37.6 kB)
avatar image dan_wipf JoniBerg · Feb 03, 2019 at 05:17 PM 0
Share

it resets with each call to 0 again, you need to make iscollected not a local variable, so it will store where it is. and then you need to reset it maybe inside of repairedisready to 0 again

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

100 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 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 to deactivate gameObjects instead of destroying them ? 1 Answer

Game Object Chains 1 Answer

Array of bool doesn't change in other script 1 Answer

Filling array with gameObjects 2 Answers

GameObject Array in Editor GUI 3 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