My tower limiting code

I did my tower limiting different to Ben as I used a list. Whenever I try to spawn, I first check the length of the list, if it is shorter than the towerLimit I spawn a tower and add that tower to the list.

    [SerializeField] private Tower towerPrefab;
    [SerializeField] private int towerLimit = 3;
    [SerializeField] private Transform parentTowersTo;
    List<Tower> placedTowers = new List<Tower>();
    public void AddTower(Waypoint baseWaypoint)
    { if (placedTowers.Count < towerLimit)
        {
            var tower = Instantiate(towerPrefab, baseWaypoint.transform.position, Quaternion.identity);
            transform.parent = parentTowersTo;
            baseWaypoint.isPlaceable = false;
            placedTowers.Add(tower);
        }
        else
        {
            print("Tower limit reached");
        }

    }
1 Like

Looking good!

1 Like

Privacy & Terms