Class value as list of the same class
Hi all, I'm trying to create a script that creates a folder tree based on a List called ItemDB. This script must get all elements in this list and create as many directories as the number of elements in that list. The problem is that every single element in this list must have another list in their properties that refers to the same class, something like this:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class FolderManager : MonoBehaviour {
[System.Serializable]
public class Item {
public string name;
public string path;
public List<Item> subItems = new List<Item>();
}
}
my question: Is it possible to have something like this or is there a way to achieve the same result but with a workaround? Many thanks to all, I hope you can point me in the right direction cause I'm lost at this point.
Not sure if I completely understand what you want. What is wrong with the example you provided?
I guess generics is the thing you want here. You can expand upon what you already have by using generics for your own class as well
public class RecursiveItem<T> {
public string name;
public string path;
public List<RecursiveItem<T>> subItems =
new List<RecursiveItem<T>>();
}
Generics always has the limitation that T must be known at compile time though.