# How can I start the game with a random formation?

public class SquadFormation : MonoBehaviour
{
enum Formation
{
Square, Circle, Triangle
}

public int columns = 4;
[Range(4, 100)]
public float yOffset = 0;
[Range(1, 20)]
[Space(5)]
public int squareSpace = 10;
public int circleSpace = 40;
[Range(3,50)]
public float moveSpeed = 3;
public bool randomSpeed = false;
[Range(3, 50)]
public float rotateSpeed = 1;
public float threshold = 0.1f;
public string currentFormation;
public bool startRandomFormation = false;

private Formation formation;
private List<Quaternion> quaternions = new List<Quaternion>();
private List<Vector3> newpositions = new List<Vector3>();
private bool move = false;
private bool squareFormation = false;
private List<GameObject> squadMembers = new List<GameObject>();
private float[] step;
private int[] randomSpeeds;
private int index = 0;
private int numofobjects = 0;

// Use this for initialization
void Start()
{
if (startRandomFormation)
{
formation = UnityEngine.Random.Range(1, 3);
}


In this case I have 3 formations. But it might later more or less formations. Making this line is has no sense:

formation = UnityEngine.Random.Range(1, 3);


The idea is to start randomly with one of the 3 formations.

How can I get the number of formations ? The count in the enum ? And how can I select randomly one formation ?

• The line to get the random enum item is: formation = (Formation)UnityEngine.Random.Range(0, 3); but how can I get the number of items in the enum ? Instead 0, 3 something like 0, Formation.Count ? – Benzi Avrumi Jun 27 '18 at 17:09
• ...did you try searching for "C# number of items in enum"? This is covered rather extensively elsewhere. – DMGregory Jun 27 '18 at 17:12

Enum.GetNames(typeof(Formation)).Length;

Your formations are certainly more than just an integer. A formation is actually a list of positions, and maybe even some information about how members of the formation behave at runtime. That means it might make sense to create a class Formation which contains all data and code specific to each formation. You can then have a Formation[] possibleFormations in your SquadFormation class.
You could even go one step further and make SquadFormation a ScriptableObject. That way you can create new squad formations as assets, edit them in a custom-made editor and assign them to your SquadFormation game objects using the inspector.