I did it my way

I approached things a bit differently than how the video showed.

I used a list instead of a queue. After the list was filled with towers (I compared the size of the list with a serialized maxTowers variable) I kept track of the location of the oldest tower with an oldestTower index into the list. After the oldest tower is used then I incremented the oldestTower variable and wrapped it around to the Zeroth position in the list as necessary. This way kept me from queuing and dequeuing the tower objects in the queue. I might go back and refactor the code inside the if statement blocks since it is right on the threshold for complexity.

    public void InstantiateTower(Vector3 spawnPosition, Waypoint cube)
    {        
        if (towerList.Count < maxTowers)
        {
            Tower newTower = Instantiate(towerPrefab, spawnPosition, Quaternion.identity);
            newTower.transform.parent = gameObject.transform;
            towerList.Add(newTower);
            wayPointList.Add(cube);
            cube.clearIsPlacable();
        }
        else
        {
            towerList[oldestTower].transform.position = spawnPosition;
            wayPointList[oldestTower].setIsPlacable();
            wayPointList[oldestTower] = cube;
            cube.clearIsPlacable();

            oldestTower += 1;
            if (oldestTower >= maxTowers)
            {
                oldestTower = 0;
            }
        }
    }

Privacy & Terms