- Home /
Question by
Ari Levi · Feb 16, 2012 at 10:04 AM ·
c#arrayconstructor
Constructor in array of classes? [SOLVED]
What is wrong with my code?
I'm trying initialise an class for every gameObject I put in an array. I pass each entry of the class array a game object for it to reference. I am trying to do this in the constructor.
I hope this make sense.
public GameObject[] button;
MeshButton[] buttonMesh; //This is the class I created
void Awake()
{
buttonMesh = new MeshButton[button.Length];
for(int i =0; i< button.Length;i++)
{
buttonMesh[i] = new MeshButton(button[i]);
buttonMesh[i].UnSelect();
}
}
Comment
Is '$$anonymous$$eshButton' a $$anonymous$$onoBehaviour? You can't use the constructor with $$anonymous$$onoBehaviours- ins$$anonymous$$d, use AddComponent and a custom 'Init' method.
Your answer
![](https://koobas.hobune.stream/wayback/20220613064136im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
C# convert Bytearray to System.Draw.Image 2 Answers
C# Incrementing a String Array 1 Answer