Menu
CoddyTech

Tower Data

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

A second tower is coming, the Cannon, with numbers of its own. As with the enemies, the stats move into an asset, TowerData, one per kind: the project has ArrowData (cost 50, damage 2, range 4.5, fireRate 1.5). The prefab keeps what the tower looks like, and its Tower script points at its asset.

[CreateAssetMenu] above a ScriptableObject class puts it in Unity's Create menu, so new assets can be made in the Editor. The enemies' asset has one:

[CreateAssetMenu(fileName = "NewEnemy", menuName = "Tower Defense/Enemy Data")]
public class EnemyData : ScriptableObject
challenge icon

Challenge

Medium

Move each tower's stats into a TowerData asset.

  1. In TowerData.cs, add [CreateAssetMenu] and the public fields towerName (a string), cost and damage (ints), range and fireRate (floats).
  2. In Tower, replace those four fields with public TowerData data; and read data.range, data.damage and data.fireRate.
  3. In BuildManager, pay towerPrefab.data.cost.

Press Play: the Arrow fights as before, now from ArrowData. Then Check.

Try it yourself

using UnityEngine;

// Comes later: the stats of one kind of tower, as an asset.
public class TowerData : ScriptableObject
{
}

All lessons in Unity Game: Tower Defense

Practice on your own: Unity playground