- Home /
help with lists inside lists
Greetings. I'm very new to c# (I have some experience with PHP as I'm working as a backend developer but new to c#)
I would like to make a list inside a list and then add to it.
I have this code:
[System.Serializable]
public class ColorToPrefab {
public Color color;
public GameObject prefab;
public float yPosition = 0f;
public ColorToPrefab (Color c, GameObject p, float yp) {
color = c;
prefab = p;
yPosition = yp;
}
}
[System.Serializable]
public class Images {
public Texture2D map;
public List<ColorToPrefab> colorMappings = new List<ColorToPrefab> ();
public Images (Texture2D m, List<ColorToPrefab> cM) {
map = m;
colorMappings = cM;
}
}
public class getfiles : MonoBehaviour {
public List<ColorToPrefab> colortoprefab = new List<ColorToPrefab>();
public List<Images> images = new List<Images>();
void Start() {
GameObject prefab = (GameObject)Resources.Load ("corner");
Texture2D texture = (Texture2D)Resources.Load ("Level/floor_1_corners");
colortoprefab.Add (new ColorToPrefab(new Color(255,0,0,255),prefab,20));
images.Add (new Images (texture, new ColorToPrefab(new Color(255,0,0,255),prefab,20)));
}
}
So my problem is: Everything is working fine with the colortoprefab, but with images I'm getting an rror no matter how I try. The first testure parameter is good, it has problems with the second parameter.
I get this error:
Assets/getfiles.cs(54,15): error CS1502: The best overloaded method match for Images.Images(UnityEngine.Texture2D, System.Collections.Generic.List)' has some invalid arguments Assets/getfiles.cs(54,36): error CS1503: Argument
#2' cannot convert ColorToPrefab' expression to type
System.Collections.Generic.List'
Can you please help me?
Answer by Cherno · Jun 25, 2017 at 01:08 PM
Try
images.Add (new Images (texture, new List<ColorToPrefab>() { new ColorToPrefab(new Color(255,0,0,255),prefab,20) }));