Clicking on tiles without enough gold bug fix

Probably will be addressed later in one of the next lectures, but if it’s not, here’s what i found out:

If you click on tiles even without the money to place a tower, you can change momentarily the path of the enemy, until other stuff in the scripts reverts it back…

I still need to figure it out what reverts it back thou, i only know it resets everytime a new enemy spawn and at this point there is too much going on between all the scripts, but if you fix the first problem this will not matter and be just a curiosity.

Here’s the fix:

first add a property to the cost of the ballista in the Towers script:

[SerializeField] int ballistaCost = 75;
    public int BallistaCost { get { return ballistaCost; } }

then go in the Tile script and in the OnMouseDown() add a reference to the Bank script (we want it here because needs to take a new reference everytime we click, currentBalance is changing constantly and reference it in awake or start won’t work, plus Unity will give you back a null reference error)

Then with an if statement, return the method if you don’t have enough gold to place a tower/ballista and thats it:

void OnMouseDown() 
    {
        Bank bank = FindObjectOfType<Bank>();                                                                                                  
        if (bank.CurrentBalance < ballistaPrefab.BallistaCost) { return; }   

        if(gridManager.GetNode(coordinates).isWalkable && !pathfinder.WillBlockPath(coordinates))
        {
            bool isSuccessful = ballistaPrefab.CreateTower(ballistaPrefab,transform.position);
            if(isSuccessful)
            {
                gridManager.BlockNode(coordinates);
                pathfinder.NotifyReceivers();
            } 
        }    
    }

I’ve tryed adding the
“bank.CurrentBalance >= ballistaPrefab.BallistaCost” in the other if statement down below plus a else return at the end, but if you click won’t stop update a new path anyway, and that’s kinda weird but surely i’m missing the reason behind it, so brute force it is.

Again, this is more a temporary fix, and in further lectures probably will be adrressed and you will have to delete these lines, or maybe it just me having this bug, but if it’s not there you go :slight_smile:

1 Like

Privacy & Terms