- Home /
2D GameObject Array to 2D List
I have a 2D GameObject array, and I want to convert it into a 2D GameObject List. Using .ToList() gives a error. Is there a way to use something like .ToList() to convert this array, or will I have to craft the list from the array myself?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class Test : MonoBehaviour {
private GameObject[][] array2D;
private List<List<GameObject>> list2D = new List<List<GameObject>>();
void Update () {
array2D = list2D.ToList ();
}
}
The error the console gives is:
Cannot implicitly convert type `System.Collections.Generic.List<UnityEngine.GameObject[]>' to `System.Collections.Generic.List<System.Collections.Generic.List<UnityEngine.GameObject>>'
There is no built in function for C# to convert an array of arrays to a list of lists, which makes sense since the structure is not an array of arrays but rather an array that happens to be filled with arrays.
Answer by jaja1 · May 27, 2015 at 01:13 AM
You are dealing with a two dimensional array and a one dimensional, resizable array (essentially, that is what a list is). I suppose you can create a custom List class to handle such a conversion but as @maccabbe said, this is not possible.
By using this plugin, create 2D list easily.
Your answer
Follow this Question
Related Questions
2D Array of GameObjects... 1 Answer
Enabled all gameobjects in array 1 Answer
Deactivate current object in array, activate next one? 2 Answers
2D array of GameObjects C# 2 Answers