pvtctrlalt
pvtctrlalt

Reputation: 61

How to check if bool is false in a loop

I'm trying to loop through an array of classes. The class has two variables: a transform and a bool.

I want to loop through this in another script to see if that current position is occupied and if so the bool occupied will be set to true.

How can I go about doing this?

 public Positions[] PosInObect = new Positions[1];

 [System.Serializable]
 public class Positions
 {
     public Transform pos;
     public bool isFilled;
 }

 for (int i = 0; i < TheObject.GetComponent<GetInObject>().PosInObect.Length; i++) 
 {

 }

Upvotes: 0

Views: 1259

Answers (2)

Syntasu
Syntasu

Reputation: 69

This can be done in a foreach loop. You will be still able to access the variables from that class instance.

foreach(Positions pos in TheObject.GetComponent<GetInObject>().PosInObect)
{
    if(pos.isFilled)
    {
       //Do something
    }
}

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500055

Well, you can just access the element at the relevant index, and then check the field value:

 if (TheObject.GetComponent<GetInObject>().PosInObect[i].isFilled)

However, if you don't need the index, I'd recommend using a foreach loop:

foreach (var position in TheObject.GetComponent<GetInObject>().PosInObect)
{
    if (position.isFilled)
    {
        ...
    }
}

And if you do need the position, I'd use a local variable to fetch the array once first:

var positions = TheObject.GetComponent<GetInObject>().PosInObect;
for (int i = 0; i < positions.Length; i++)
{
    if (positions[i].isFilled)
    {
        ...
    }
}

I'd also recommend using properties instead of public fields, and following .NET naming conventions.

Upvotes: 9

Related Questions