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 : ScriptableObjectChallenge
MediumMove each tower's stats into a TowerData asset.
- In
TowerData.cs, add[CreateAssetMenu]and the public fieldstowerName(a string),costanddamage(ints),rangeandfireRate(floats). - In
Tower, replace those four fields withpublic TowerData data;and readdata.range,data.damageanddata.fireRate. - In
BuildManager, paytowerPrefab.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