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

    [Header("Main Settings")]
    public Transform squadMemeberPrefab;
    public int columns = 4;
    [Range(4, 100)]
    public int numberOfSquadMembers = 20;
    public float yOffset = 0;
    [Range(1, 20)]
    public int numberOfSquads = 1;
    public int squareSpace = 10;
    public int circleSpace = 40;
    public float moveSpeed = 3;
    public bool randomSpeed = false;
    [Range(3, 50)]
    public float rotateSpeed = 1;
    public float threshold = 0.1f;
    public bool destroySquad = false;
    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()
        numofobjects = numberOfSquadMembers;
        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 ?

  • \$\begingroup\$ 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 ? \$\endgroup\$
    – Daniel Lip
    Jun 27, 2018 at 17:09
  • 1
    \$\begingroup\$ ...did you try searching for "C# number of items in enum"? This is covered rather extensively elsewhere. \$\endgroup\$
    – DMGregory
    Jun 27, 2018 at 17:12

1 Answer 1


This question essentially boils down to "how do I get the number of elements in an enum in C#", which is already answered on stackoverflow:


But I would recommend a different approach than an enum for this situation.

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.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .