Game objects not being added to queue

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
    }
}

Hi Ishaan,

Please add Debug.Logs to your if- and else-block to see which block gets executed. Maybe the if-block never got executed. In that case, check the value of maxTowers via a Debug.Log.

(I also replied in your other thread.)

The if statement gets executed as the towers do get instantiated (I checked with a debug statement too. It runs)

And the maxTowers value during runtime?

Always returns 0

Check the value of the variable in your Inspector. What’s it value there?

It stays 0 as well

I think you found the issue. Stop your game and increase the value of that variable in your game.

I placed a tower down and changed the currentTower value to 1. When I placed another tower, it changed back to 0

Could you upload your project (without the Temp and the Libary folders) to GitHub and share a link to your public repository here? I’d like to take a look at it after the weekend.

Here is a helpful tutorial by Brackeys:

Here you go

I think I found the culprit: The Tower prefab has got a TowerFactory attached. When logging GetInstanceID() of the TowerFactory into your console, you’ll notice that the id is different. This means that each time you click, the AddTower method of another object gets executed.

Remove the TowerFactory from the Tower prefab.

1 Like

It worked! I removed the script from the prefab and added it to a new empty game object and it’s working now! Thank you so much!!!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms