How can i create a squad of soldiers in specific formation ?

I tried this script but it’s not working.
For now i have only one formation square.
I want for example if there are 4 columns and space 10 and 20 soldiers so the soldiers will be in formation of 4 soldiers in a row(column) and 5 lines.

And if for example in the loop i will make 23 so there will be 4 soldiers in a row/column and 6 lines the last line will be with only 3 soldiers.

In example1 it’s a case of 4 soldiers in a column and 5 lines(columns).

In this example2 there are 23 soldiers so the last lines have only 3 soldiers. This is how the squarre format should be.

The script i tried:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SquadFormation : MonoBehaviour
{
    enum Formation
    {
        Square
    }

    public Transform squadMemeber;
    public int columns = 4;
    public int space = 10;

    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }

    private void ChangeFormation()
    {
        Formation formation = Formation.Square;

        switch (formation)
        {
            case Formation.Square:
                for (int i = 0; i < 20; i++)
                {
                    Transform go = Instantiate(squadMemeber);
                    Vector2 pos = CalcPosition(i);
                    go.position = new Vector2(pos.x, pos.y);
                }
                break;
        }
    }

    Vector2 CalcPosition(int index) // call this func for all your objects
    {
        float posX = (index % columns) * space;
        float posY = (index / columns) * space;
        return new Vector2(posX, posY);
    }
}

But it does nothing. Not event creating the soldiers.
I’m not getting any errors or exceptions.
And not sure if i should use Vector2 or Vector3 but it’s not working at all. Even not duplicate/creating the soldiers.

You’re not calling the ChangeFormation() function for starters

Hi All,

For learning purposes, is there anything wrong if i do it this way?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour
{
    enum Formation
    {
        Square
    }

    
    public GameObject squadMember;
    public int columns = 4;
    public int space = 3;

    // Use this for initialization
    void Start()
    {
        ChangeFormation();

    }

    // Update is called once per frame
    void Update()
    {
        
    }

    private void ChangeFormation()
    {
        Formation formation = Formation.Square;

        switch (formation)
        {
            case Formation.Square:
                for (int i = 0; i < 20; i++)
                {               
                    Instantiate(squadMember);
                    Vector3 pos = CalcPosition(i);
                    squadMember.transform.position = new Vector3(transform.position.x + pos.x, 0, transform.position.y + pos.y);
                    
                }
                break;
        }
    }

    Vector3 CalcPosition(int index) // call this func for all your objects
    {
        float posX = (index % columns) * space;
        float posY = (index / columns) * space;
        return new Vector2(posX, posY);
    }
}