Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How can I add already existing bools to a list?

Discussion in 'Scripting' started by LazyGhost15, Jul 18, 2023.

  1. LazyGhost15

    LazyGhost15

    Joined:
    Feb 12, 2022
    Posts:
    88
    I want to add already existing bools (for example IsMoving and IsJumping) to a bool list but I don't know how.
     
  2. Yuchen_Chang

    Yuchen_Chang

    Joined:
    Apr 24, 2020
    Posts:
    104
    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).
     
  3. LazyGhost15

    LazyGhost15

    Joined:
    Feb 12, 2022
    Posts:
    88
    I want to use the for loop to change each bool on the list
     
  4. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    521
    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?
     
    Kurt-Dekker likes this.
  5. LazyGhost15

    LazyGhost15

    Joined:
    Feb 12, 2022
    Posts:
    88
    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
     
  6. ijmmai

    ijmmai

    Joined:
    Jun 9, 2023
    Posts:
    188
    How many boolean variables do you have?

    If you want multiple variables to get the same value, you could do something like
    Code (CSharp):
    1. isMoving = isJumping = isGreen = isVisible = true;
     
  7. LazyGhost15

    LazyGhost15

    Joined:
    Feb 12, 2022
    Posts:
    88
    I want to do something like this:

    Code (CSharp):
    1. if(object at position[i] exist)
    2. {
    3. bool[i] == true
    4. }
    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:
    Code (CSharp):
    1.     Vector3[] Positions = {new Vector3(vector), new Vector3(vector), new Vector3(vector), new Vector3(vector), new Vector3(vector)};
    2.  
     
  8. tsukimi

    tsukimi

    Joined:
    Dec 10, 2014
    Posts:
    50
    List also supports
    []
    operator. Just initialize at start, then check and set the value.
    Code (CSharp):
    1.  
    2. private List<bool> boolList = new();
    3. private int positionCount = 0; // your max position count
    4.  
    5. private void Start()
    6. {
    7.     // initialize the bool list to the same count as position
    8.     for (var i = 0; i < positionCount; i++) boolList.Add(false);
    9. }
    10.  
    11. private void Update()
    12. {
    13.     // do your check here, and set the cell to true
    14.     if (/*object at position[i] exist*/) boolList[i] = true;
    15. }
     
  9. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    521
    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 o_O
     
  10. ijmmai

    ijmmai

    Joined:
    Jun 9, 2023
    Posts:
    188
    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.
     
  11. LazyGhost15

    LazyGhost15

    Joined:
    Feb 12, 2022
    Posts:
    88
    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.
     
  12. ijmmai

    ijmmai

    Joined:
    Jun 9, 2023
    Posts:
    188
    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
     
  13. LazyGhost15

    LazyGhost15

    Joined:
    Feb 12, 2022
    Posts:
    88
    The second one is more accurate.
     
  14. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    521
    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#.
     
  15. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,084
    Code (csharp):
    1. public struct MyBools
    2. {
    3.     public bool IsMoving;
    4.     public bool IsJumping;
    5. }
    6.  
    7. // Create the list
    8. public List<MyBools> bools = new List<MyBools>();
    9.  
    10. // Example of how to add to the list
    11. var someBools = new MyBools { IsMoving = movingValue, IsJumping = jumpingValue };
    12. bools.Add(someBools);
    13.  
    14. // Examples of how to access them in the list
    15. var entry = bools[0];
    16. Debug.Log($"Are we moving? {entry.IsMoving}");
    17. Debug.Log($"Are we jumping? {entry.IsJumping}");