- Home /
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?
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
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.
Can you give us more ? we can't see what are isRepaired and partOfToys
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);
}
}
}
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 }
Yeah this Linq working on repairedIsReady() method.
But what working checking for all booleans for one by one?
then best practice is a forloop, or you check if any(linqish) way if any part isnt repaired
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.
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
you working with array in foreach loop? this index [mypartarray.length-1] take only last index in array.
This is so simple problem how i set one bool true if one part is collected?
This is infuriating. :D
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 :(
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.
}
}
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.
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
Follow this Question
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