I asked the same question over in the QnA (https://www.udemy.com/course/unitycourse2/learn/lecture/9726554#questions/10932716) but the answer there didn’t really work for me, so I’m asking it again over here to get more eyes onto it.
My code is the same as Ben’s but when I instantiate the tower, it does not get added to the queue. When printing out the queue.Count, it always returns as 0 and due to that, my currentTowers number stays 0 too. Can anyone help me out? Here’s the code:
public class TowerFactory : MonoBehaviour
{
[SerializeField] int maxTowers = 3;
[SerializeField] Tower towerPrefab;
Queue<Tower> towerQueue = new Queue<Tower>();
public void AddTower(Waypoint baseWaypoint) {
print(towerQueue.Count);
int currentTowers = towerQueue.Count;
if (currentTowers < maxTowers)
{
InstantiateNewTower(baseWaypoint);
}
else {
MoveExistingTower(baseWaypoint);
}
}
void InstantiateNewTower(Waypoint baseWaypoint) {
var newTower = Instantiate(towerPrefab, baseWaypoint.transform.position, Quaternion.identity);
baseWaypoint.isPlaceable = false;
// set the baseWaypoints
towerQueue.Enqueue(newTower);
}
void MoveExistingTower(Waypoint baseWaypoint) {
// take bottom tower off queue
// set the placeable flags
// set the baseWaypoints
// put old tower back on the top of the queue
}
}