How can I add already existing bools to a list?

I want to add already existing bools (for example IsMoving and IsJumping) to a bool list but I don’t know how.

Just use list.Add(IsMoving), but what do you want to achieve? Do you want the bool inside the list syncs with the IsMoving? If so, you need to create a wrapper class for your IsMoving and store the wrapper in the list, since bool is ValueType (= you can only get a copy the value of the bool, in most situations).

I want to use the for loop to change each bool on the list

Something doesn’t make sense and a list of Boolean values seems like an odd solution. Care to offer a bit more detail on the goal?

I want to change a cube sprite by its surroundings, so I thought ill make a loop that checks each position in the array and then if exists, will change bool true

How many boolean variables do you have?

If you want multiple variables to get the same value, you could do something like

isMoving = isJumping = isGreen = isVisible = true;

I want to do something like this:

if(object at position[i] exist)
{
bool[i] == true
}

To do that I need to get the bools into a list.
Also is there a way to add a bool to the list without the list.Add? like in array that I can do:

    Vector3[] Positions = {new Vector3(vector), new Vector3(vector), new Vector3(vector), new Vector3(vector), new Vector3(vector)};

List also supports [ ] operator. Just initialize at start, then check and set the value.

private List<bool> boolList = new();
private int positionCount = 0; // your max position count

private void Start()
{
    // initialize the bool list to the same count as position
    for (var i = 0; i < positionCount; i++) boolList.Add(false);
}

private void Update()
{
    // do your check here, and set the cell to true
    if (/*object at position[i] exist*/) boolList[i] = true;
}

Sorry but it still isn’t making sense to me. Why a list of Booleans and you are referencing it like an array. Is there no way to turn these settings into a simple class with named properties and perhaps a method that encapsulates whatever logic you need?

I’m confused :eyes:

With code almost everything is possible, the question is, is it the best/right way to go.
If you could tell more about your game/setup, it would help to provide you with better advice.

All we know right now is, you have boolean variable and objects. That is not much to go on.

I want to make my cube change its sprite if there are other cubes around it, or above it. So I want to make a for loop that goes through each direction (left, right, up, forward, and backward), and then if there is a cube in the direction, then the bool of the direction changes to true.

something like:

  • green cube
  • check for cubes near
  • if so, change to blue

or more like

  • green cube
  • if other cube to the left, change to purple
  • if other cube to the right, change to yellow

The second one is more accurate.

Then I have to say I don’t see the point of a list of Booleans at all. Many ways to accomplish it but your cube can seemingly check around itself and determine which sprite to set accordingly.

One way to approach such problems is to write pseudocode (more English than C#) and see if the logic “works”. Easier to revise pseudocode than C#.

public struct MyBools
{
    public bool IsMoving;
    public bool IsJumping;
}

// Create the list
public List<MyBools> bools = new List<MyBools>();

// Example of how to add to the list
var someBools = new MyBools { IsMoving = movingValue, IsJumping = jumpingValue };
bools.Add(someBools);

// Examples of how to access them in the list
var entry = bools[0];
Debug.Log($"Are we moving? {entry.IsMoving}");
Debug.Log($"Are we jumping? {entry.IsJumping}");