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 make a list for an array of bools (bool[])

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

  1. LazyGhost15

    LazyGhost15

    Joined:
    Feb 12, 2022
    Posts:
    88
    I want to make a number of bool serieses in the same list (I want an array of bools inside of a list) how can I do this?
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,507
    Same way as you would anything else in a list:
    • Create the List
    • Add the things to the List

    Code (CSharp):
    1. var list = new List<bool[]>();
    2. list.Add(new bool[] { true, false, false });
    3. list.Add(new bool[] { false, true });
     
  3. ijmmai

    ijmmai

    Joined:
    Jun 9, 2023
    Posts:
    188
  4. LazyGhost15

    LazyGhost15

    Joined:
    Feb 12, 2022
    Posts:
    88
  5. LazyGhost15

    LazyGhost15

    Joined:
    Feb 12, 2022
    Posts:
    88
    Can I also do:

    Code (CSharp):
    1. list<bool[]> list = new list<bool[]>();
    2. list.Add(new bool[] {true, false, false})
    ?
    Just without the
    var
     
  6. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,355
    Yeah, it's the same thing.
     
  7. LazyGhost15

    LazyGhost15

    Joined:
    Feb 12, 2022
    Posts:
    88
    So I don't need the 'var' thing? So what is it for?
     
  8. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,124
    Your intent is different but the code isn't really going to change from what I posted in that thread. Adding a new entry to a list is the same as adding an existing one.

    It's a way of declaring a variable in a method without directly specifying the type.
     
  9. LazyGhost15

    LazyGhost15

    Joined:
    Feb 12, 2022
    Posts:
    88
    Oh, I didn't know it was the same code because I didn't know how to even do this, I thought it was different considering that several arrays inside a list have many more bools than a list with several individual bools. And instead of adding I am creating arrays of bools from scratch.
     
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,507
    Ryiah likes this.
  11. LazyGhost15

    LazyGhost15

    Joined:
    Feb 12, 2022
    Posts:
    88
    I searched on Google but I couldn't find people that put arrays of bools on a list, only regular bools. Maybe I searched the wrong way.
     
  12. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,507
    The thing is, you're making this a special case when it's not. An array of bools, a single bool, another list, all can be added because it's a generic type.

    List<T> means a list of type T. T can be a simple type like int, bool, float or fixed arrays like int[], bool[], float[] etc or even other lists like List<bool>, List<int> etc or even classes or structs.

    I think you should work your way through some fundamentals in C# which will make things a lot easier.
     
    Kurt-Dekker likes this.
  13. sngdan

    sngdan

    Joined:
    Feb 7, 2014
    Posts:
    1,131
    It remains unclear what you want to do, but either it’s plain c# and pretty basic or if you are still trying to solve the problem described in the other thread, you might want to go down another route (OverlapSphere, then you go through the returned array and pick up the info from the array of returned colliders that you need to set your color - this is of course not optimized but I think you need something simple first…)
     
    Chubzdoomer likes this.