- Home /
The question is answered, right answer was accepted
C# Array of OtherArray's Meshes
Hi everyone, I've been trying to create an Array of SomeTransformArray's Meshes. I'm getting an error from the console saying Transform[] doesn't contain a definition of GetComponent. What is wrong with my code?
public Transform[] someTransformArray;
public Mesh[] someTransformArrayMeshes;
void Awake(){
someTransformArray = GetComponentsInChildren<Transform>();
for (int a = 0; a < someTransformArray.Length; a++)
someTransformArrayMeshes = someTransformArray.GetComponent<MeshFilter>().sharedMesh[a];
}
Answer by Armand · Jun 25, 2013 at 08:33 AM
someTransformArrayMeshes and someTransformArray are both declared as arrays, which as the error message says do not contain a definition for GetComponent, and in any case you should specify the particular object you want to access in the array. So you should change your line 9 to something like:
someTransformArrayMeshes[a] = someTransformArray[a].GetComponent<MeshFilter>().sharedMesh;
I'm getting a null reference exception from the console. It says the error is co$$anonymous$$g from line 9 but I don't understand why.
public Transform[] someTransformArray;
public $$anonymous$$esh[] someTransformArray$$anonymous$$eshes;
void Awake(){
someTransformArray = GetComponentsInChildren<Transform>();
for (int a = 0; a < someTransformArray.Length; a++)
someTransformArray$$anonymous$$eshes = someTransformArray$$anonymous$$eshes[a] = someTransformArray[a].GetComponent<$$anonymous$$eshFilter>().shared$$anonymous$$esh;
}
I tried using the same method that I used for my someTransformArray and it worked.
public $$anonymous$$eshFilter[] someTransformArray$$anonymous$$eshes;
void Awake(){
someTransformArray = GetComponentsInChildren<$$anonymous$$eshFilter>();
}
Try replacing your line 9 with the exact line I gave you, your version still has some unnecessary extra that's probably causing the errors.
What you're doing there is assigning a single object from the array as the entire array - that makes no sense. I think you might need to revise how arrays work, try for example http://en.wikibooks.org/wiki/Data_Structures/Arrays and http://msdn.microsoft.com/en-us/library/aa288453(v=vs.71).aspx