- Home /
adding components to a gameObject in a list of lists
im trying to add components to a gameobject thats inside a list of lists,
im getting the error
Assets/createplane.cs(151,71): error CS1061: Type System.Collections.Generic.List' does not contain a definition for
AddComponent' and no extension method AddComponent' of type
System.Collections.Generic.List' could be found (are you missing a using directive or an assembly reference?)
the error is on the line
MeshFilter meshFilter = (MeshFilter)thePlanes[(int)z].AddComponent(typeof(MeshFilter));
i think im not accessing the .addcomponent of the gameobject in the list properly, but im not sure,
List<List<GameObject>> thePlanes;
void Awake()
{
List<List<GameObject>> thePlanes = new List<List<GameObject>>();
for(float i = 0;i<planecount;i++)
{
thePlanes.Add(new List<GameObject>());
for(float j = 0;j<planecount;j++)
{
MakePlane(i,j);
}
}
}
void MakePlane(float x, float z)
{
//GameObject plane = new GameObject("Plane x="+x+" z="+z);
thePlanes[(int)z].Add(new GameObject("Plane x="+x+" z="+z));
//MeshFilter meshFilter = (MeshFilter)plane.AddComponent(typeof(MeshFilter));
MeshFilter meshFilter = (MeshFilter)thePlanes[(int)z].AddComponent(typeof(MeshFilter));
meshFilter.mesh = CreatePlane(size);
//MeshRenderer renderer = plane.AddComponent(typeof(MeshRenderer)) as MeshRenderer;
MeshRenderer renderer = (MeshRenderer)thePlanes[(int)z].AddComponent(typeof(MeshRenderer));
renderer.material.shader = Shader.Find ("Particles/Additive");
Texture2D tex = new Texture2D(1, 1);
tex.SetPixel(0, 0, Color.green);
tex.Apply();
renderer.material.mainTexture = tex;
renderer.material.color = Color.green;
meshFilter.transform.Translate(new Vector3((x*size*(size-1)),0,(z*size*(size-1))));
//thePlanes[(int)z].Add(plane);
}
i think ive got it correct, im doing it the same way i was doing it before i put them in the list, but its not working for this
What is not working ? Do you have an error or the behavior is not what you expected ?
Answer by Slavrix · Feb 23, 2014 at 03:06 PM
I solved my problem, I wasn't referencing the correct item in the list, in several places.
void Awake()
{
thePlanes = new List<List<GameObject>>();
for(float i = 0;i<planecount;i++)
{
thePlanes.Add(new List<GameObject>());
for(float j = 0;j<planecount;j++)
{
MakePlane(i,j);
}
}
}
void MakePlane(float x, float z)
{
//GameObject plane = new GameObject("Plane x="+x+" z="+z);
thePlanes[(int)x].Add(new GameObject("Plane x="+x+" z="+z));
//MeshFilter meshFilter = (MeshFilter)plane.AddComponent(typeof(MeshFilter));
MeshFilter meshFilter = (MeshFilter)thePlanes[(int)x][(int)z].AddComponent(typeof(MeshFilter));
meshFilter.mesh = CreatePlane(size);
//MeshRenderer renderer = plane.AddComponent(typeof(MeshRenderer)) as MeshRenderer;
MeshRenderer renderer = (MeshRenderer)thePlanes[(int)x][(int)z].AddComponent(typeof(MeshRenderer));
renderer.material.shader = Shader.Find ("Particles/Additive");
Texture2D tex = new Texture2D(1, 1);
tex.SetPixel(0, 0, Color.green);
tex.Apply();
renderer.material.mainTexture = tex;
renderer.material.color = Color.green;
meshFilter.transform.Translate(new Vector3((x*size*(size-1)),0,(z*size*(size-1))));
//thePlanes[(int)z].Add(plane);
}
Answer by Bunnybomb7670 · Feb 23, 2014 at 02:39 PM
Try doing MeshFilter meshFilter = ((MeshFilter)thePlanes[(int)z]).AddComponent(typeof(MeshFilter));
Your answer
Follow this Question
Related Questions
Why does the script don't apply to the other duplicated player 0 Answers
How to find all similar elements in a list? 1 Answer
How do i activate a function attached to a listed GameObject from the perspective of itself? 1 Answer
Gather all GameObjects with a certain name in a Scene and Apply them to a list? 3 Answers