My very simple ring buffer

public class TowerRingBuffer : MonoBehaviour
{
    [SerializeField] GameObject towerPrefab;
    Queue<GameObject> towers = new Queue<GameObject>();
    Vector3 farAway = new Vector3(0, -1000, 0);
    int qSize = 4;
    void Start()
    {
        for (var index = 0; index < qSize; index++)
        {
            GameObject t = Instantiate(towerPrefab, farAway, Quaternion.identity, gameObject.transform);
            towers.Enqueue(t);
        }
    }
    public void MoveTower(Vector2Int pos)
    {
        Vector3 newPos = new Vector3(pos.x * 10, 0, pos.y * 10);
        foreach (var tower in towers) { if (tower.transform.position == newPos) return; }  //bail if occupied

        GameObject towerObject = towers.Dequeue();
        towerObject.transform.position = newPos;
        towers.Enqueue(towerObject);
        return;
    }
}

Privacy & Terms