I have problems with List<List<Colliders>>

Hi! I have a strange behavior with _allHits= List<List> when adding new List to _allHits and also when removing Lists from _allHits.

I don’t quite understand why I have to create a new List with the values from _hitsPreview when I want to add the list to _allHits.

When I try to remove a List from _allHits, all the values from the last List [_allHits.Count - 1] are cleaned but
_allHits still have the same allHits.Count.

I would be glad if someone can explain to me what the hell is happening.=)) thx for your help!

private void PreviewCommands()
    {
        if(MaximumNumberOfCommands())
        {
            GetComponent<HandleCommands>().HandleTouch();
        }
    }
    private void ExecuteCommand()
    {
        if (MaximumNumberOfCommands())
        {
            _allHits.Add(new List<Collider2D>(GetComponent<HandleCommands>().HitsPreview));
            //_allHits.Add(GetComponent<HandleCommands>().HitsPreview);
            GetComponent<HandleCommands>().HitsPreview.Clear();
            GetComponent<ExecuteCommands>().ExecuteCommand(_allHits);
        }
    }
    private void DeleteCommand()
    {
        _allHits.RemoveAt(_allHits.Count - 1);
        //GetComponent<DeleteCommands>().DeleteCommand(_allHits);
    }
    private bool MaximumNumberOfCommands()
    {
        return _allHits.Count < robot.actionNumber;
    }
    private void Update()
    {
        foreach (List<Collider2D> item in _allHits)
        {
            print(_allHits.IndexOf(item) + "  " + item.Count);
        }
    }

Once you get into lists-of-lists it’s hard to reason about any code by staring at it.

To help gain more insight into your problem, I recommend stripping the input dataset down to the fewest number of things possible that show your issue.

Once you have done that, start liberally sprinkling Debug.Log() statements through your code to display information in realtime, such as names of items involved (give all objects unique names).

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

If I understood your question correctly then you are talking about line 12-14 inside the ExecuteCommand method?

One part of your question is why you have to create a new list at line 12 instead of adding the HitsPreview list directly like in the commented line 13?
At line 14 you clear the HitsPreview list, so if you would add the HitsPreview instance directly to the _allHits list it would also be cleared, because it is the same reference / instance.

If that was not you question then you have to add more details.