Menu
CoddyTech

Tower Levels

Lesson 16 of 25 in Coddy's Unity Game: Tower Defense course.

Towers will be upgraded: each level adds damage and range, and each upgrade costs more than the last. The levels follow a rule, so the asset holds the rule rather than a list of numbers, and methods on the ScriptableObject work it out, so every script that asks gets the same answer. A farm game's crop:

public int YieldAt(int level)
{
    return baseYield + yieldPerLevel * (level - 1);
}

Level 1 is the base value. The tower keeps only its own level, and fires with the numbers for that level.

challenge icon

Challenge

Medium

Give towers levels.

  1. In TowerData, add the public ints maxLevel, upgradeCost and damagePerLevel and the public float rangePerLevel.
  2. Add DamageAt(int level): damage + damagePerLevel * (level - 1); RangeAt the same; UpgradeCost(int level): upgradeCost * level.
  3. In Tower, add private int level = 1;, public int Level => level; and public void Upgrade() { level++; }; fire with DamageAt(level) within RangeAt(level).

Press Play: the towers fight as before. Then Check.

Try it yourself

using UnityEngine;

[CreateAssetMenu(fileName = "NewTower", menuName = "Tower Defense/Tower Data")]
public class TowerData : ScriptableObject
{
    public string towerName;
    public int cost = 50;
    public int damage = 2;
    public float range = 4f;
    public float fireRate = 1f;
}

All lessons in Unity Game: Tower Defense

Practice on your own: Unity playground