Initializing an array of Vector3 in c sharp #

Currently I’m initializing an array of Vector3 in C# like so :

Vector3[] positionArray = new Vector3[4];
positionArray[0] = new Vector3(0.0f,0.0f,0.0f);			
positionArray[1] = new Vector3(0.1f,0.1f,0.1f);
positionArray[2] = new Vector3(0.2f,0.3f,0.4f);
positionArray[3] = new Vector3(0.5f,0.5f,0.5f);

I’ve tried initializing the array using shorter methods, but all have failed. Heres an example of one that I tried :

Vector3[] positionArray = new Vector3((0f,0f,0f),(1f,1f,1f));

And here is the error that Unity reports back :

A field or property UnityEngine.Vector3' cannot be initialized with a collection object initializer because type UnityEngine.Vector3’ does not implement `System.Collections.IEnumerable’ interface

The other way to initialize the array is like this:

   Vector3[] positionArray = new [] { new Vector3(0f,0f,0f), new Vector3(1f,1f,1f) };

You can also use object initializer syntax:

Vector3[] positions = { new Vector3 { x = 0, y = 0, z = 0 }, 
                        new Vector3 { x = 1, y = 1, z = 1} };

In this case using the constructor (as Mike did) is more concise.

Or, if you need fixed array length for copying (like you would use in mesh.vertices) you could do this:

var newVerts = new Vector3[verts.Length - 10];