Creating a time delay until able to purchase another tower for TD

Hi all,

Surprisingly been struggling to find and answer to which is probably a pretty common question. I’m making a game that is very similar to plant vs zombies (which is a TD) and I need to make it so there is a set time delay until I can place another tower. Quite new to coding and game dev and I’ve no idea how to go about this. Any advice will be appreciated.

Add your delay to Time.time, and check it in Update:

public float delaySeconds = 10f;
private bool canBuild = true;
private float buildTimeout = -1f;

public void BuildSomething()
{
    if(canBuild)
    {
        Debug.Log("building...");
        StartDelay();
    }
    else
    {
        Debug.Log("not yet!");
    }
}

public void StartDelay()
{
    canBuild = false;
    buildTimeout = Time.time + delaySeconds;
}

void Update()
{
    if(!canBuild && Time.time >= buildTimeout)
    {
        canBuild = true;
    }
}
1 Like

Hi MV10 thank you, how can I make it so it does this based on individual towers rather than a delay for placing any.

That’s going to depend very heavily on the way you design your game. You’re going to need to read up on arrays and the various collection classes (like Lists and Dictionaries). Probably you’d create some type of class that describes all the properties for a certain tower (like this delay information, but other stuff like how much damage it can take and has already taken, for example), then store that in some kind of array or collection, and loop through those lists checking the timeout for each of them.

1 Like