0
\$\begingroup\$
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FormationsManager : MonoBehaviour
{
    public GameObject squadMemeberPrefab;
    public int numberOfSquadMembers = 20;
    public int numberOfSquads = 1; 
    public int columns = 4;
    public int gaps = 10;
    public Formations formations;

    private int numofmembers;
    private int numofsquads;
    private GameObject squadToClone;
    private List<GameObject> SquadMembers = new List<GameObject>();

    // Use this for initialization
    void Start()
    {
        squadToClone = GameObject.Find("Squad");
        numofmembers = numberOfSquadMembers;
        numofsquads = numberOfSquads;
        formations.Init(numberOfSquadMembers, columns, gaps);
        GenerateSquad();
    }

    // Update is called once per frame
    void Update()
    {
        if (numofmembers != numberOfSquadMembers)
        {
            numofmembers = numberOfSquadMembers;
            formations.Init(numberOfSquadMembers, columns, gaps);
            GenerateSquad();
        }

        if (numofsquads != numberOfSquads)
        {
            numofsquads = numberOfSquads;
            for (int i = 0; i < numberOfSquads; i++)
            {
                Instantiate(squadToClone);
            }
        }
    }

    private void GenerateSquad()
    {
        GameObject go = squadMemeberPrefab;
        List<GameObject> newSquadMembers = new List<GameObject>();

        int i = 0;
        for (; i < formations.newpositions.Count; i++)
        {
            if (i < SquadMembers.Count)
                go = SquadMembers[i];
            else
                go = Instantiate(squadMemeberPrefab);
            go.transform.position = formations.newpositions[i];
            go.tag = "Squad Member";
            go.transform.parent = gameObject.transform;
            newSquadMembers.Add(go);
        }

        for (; i < SquadMembers.Count; i++)
            Destroy(SquadMembers[i]);

        SquadMembers = newSquadMembers;
    }
}

In the Update I duplicate the squad:

if (numofsquads != numberOfSquads)
            {
                numofsquads = numberOfSquads;
                for (int i = 0; i < numberOfSquads; i++)
                {
                    Instantiate(squadToClone);
                }
            }

The problem is that if for example when running the game there is 1 squad and then I'm changing the value of numberOfSquads to 3 then instead duplicating more 2 squads it will duplicate more 3. And then if I will change the value of numberOfSquads from 3 to 4 it will add more 4 squads instead only 1.

And I also want to make that if I change the value of numberOfSquads to be lower then destroy the last squad/s.

\$\endgroup\$
1
\$\begingroup\$

In your loop you create a new squad for every numOfSquads. So if you want a total of 3 then you instantiate an extra 3.

Instead set the loop to start at 1(or current squad count) instead of 0 so it skips the amount you already have.

if (numofsquads != numberOfSquads)
        {

            for (int i = numofsquads; i < numberOfSquads; i++)
            {
                Instantiate(squadToClone);
            }
            numofsquads = numberOfSquads;
        } 
\$\endgroup\$
  • \$\begingroup\$ Am I missing something or should this be i = numofsquads, and then set numofsquads = numberOfSquads at the end? Like this, it would instantiate one less gameobject than in the question, but have the same problem. \$\endgroup\$ – Christian Jun 29 '18 at 10:15
  • \$\begingroup\$ Yes, i should equal the number of squads you already have. This means if you have 3 squads and want 6 the loop will loop from 3 to 5. In the first iteration i will be 3 and create a squad, the next iteration will be 4 and the final iteration will be 5. Giving you 3 extra squads to make the 6. \$\endgroup\$ – dfour Jun 29 '18 at 10:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.