Hi, I searched on the internet about this problem but the answers didn't help, so I'd like to ask you how to avoid this error. (I'm a beginner so please don't lynch me if it's a stupid question xD).
I've created a class (Figure) which has a name, an int order and a transform and it has a constructor to give each figure its name and transform.
public class Figure
{
public int order;
public string name;
public Transform tr;
public Figure(string name_, Transform tr_)
{
this.name = name_;
this.tr = tr_;
}
}
I've declared 4 public transforms that I give to 4 figures, and then I created an array that I'll use later with those figures.
public Transform SquareTR;
public Transform TrapezoidTR;
public Transform CircleTR;
public Transform TriangleTR;
Figure SquareF = new Figure("Square", SquareTR);
Figure TrapezoidF = new Figure("Trapezoid", TrapezoidTR);
Figure CircleF = new Figure("Circle", CircleTR);
Figure TriangleF = new Figure("Triangle", TriangleTR);
Figure[] Figures = { SquareF, TrapezoidF, CircleF, TriangleF };
rest of the code...
When I start the game, the error occurs: "A field initializer cannot reference the non static field, method, or property", for each transform and figure.
I could fix this problem creating the figures in the Start() function, but I have to use them in the Update() function too.
I could also make figures and transforms static, but I have to assign the transforms from the inspector (If I don't do that the NullReferenceException pops up).
How can I fix this problem?
↧