Creating a class wide array (c#)

Hi everyone,

I have to create an array that i can use in in several functions, but if i want to create it I get an error

public class PosChanged : MonoBehaviour {

private int changeCount = 0;
private int breakForce = 20;
Vector3[] v = new Vector3[transform.childCount];
Vector3[] v_old = new Vector3[transform.childCount];
.....

I know I would have to do it in Start() because of the transform.childCount, but I cant access it then in FixedUpdate() if its created in Start().

So how do I do that?

Errormsg:

(8,35): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `UnityEngine.Component.transform’

You have to declare the arrays as class members, but you initialize them in Start:

public class PosChanged : MonoBehaviour {

    private int changeCount = 0;
    private int breakForce = 20;
    Vector3[] v;
    Vector3[] v_old;

    void Start()
    {
        v = new Vector3[transform.childCount];
        v_old = new Vector3[transform.childCount];            
    }

Then you can access them inside other methods such as FixedUpdate:

    void FixedUpdate()
    {
        v[0] //...
    }