I am creating a tower defense game.

Currently I have 2 different tower types 'Shooting tower', amd 'Unit tower'.

I have a Scriptable object to initialize the tower. It contains some tower properties like its attack radius, damage and so on.

I am initializing my towers from a ScriptableObject called TowerSO, it also contains a prefab for a tower.

The problem here is that I have different properties for different tower types stored in TowerSO, for example, the unit tower has properties like spawnDelay, maximumUnitsToSpawn, while the archer tower doesn't need this info.

So I guess it is not good approach because anytime I add a new tower type with additional properties it would mean that TowerSO will grow more.

I also want to be able to initialize each tower when it's Instantiated but now I don't know what tower I am instantiating and I don't want to add specialized conditional logic for each type like if (towerType == ArcherTower)

Currently my code looks like this:

GameObject tower = Instantiate(
            new Vector3(gridPosition.x + xOffset, gridPosition.y + yOffset, currentlySelectedTowerSO.prefab.transform.position.z),

// initialize knight tower(if it is a knight tower)
tower.GetComponent<KnightTower>().Initialize(maxUnitNumber, spawnDelay);
// initialize archer tower(if it is an archer tower)
            tower.GetComponent<KnightTower>().Initialize(minDamage, maxDamage, arrowSpeed);

How do I separate different tower properties to different Scriptable Objects with completely different fields, and also how do I initialize them, passing relative arguments to each tower?

  • For ArcherTower (min-max damage, arrowSpeed...)

  • For UnitTower(MaxUnitNumber, SpawnDelay)

  • 2
    \$\begingroup\$ Don't overcomplicate it. Make a prefab for each tower with all the data it needs. This is way less error prone, faster in develop and you can add new towers by just making new prefabs. You can still have a base tower prefab and a scriptable object for things that are common for all your towers (price, image, size, etc) \$\endgroup\$
    – Zibelas
    Mar 1, 2023 at 9:33
  • \$\begingroup\$ You may be interested in Applying upgrades to pickups that do different things. There I show a way you can create stat upgrades that are agnostic about what underlying type is receiving the upgrade. \$\endgroup\$
    – DMGregory
    Mar 1, 2023 at 13:39
  • \$\begingroup\$ Discussion betwen Zibelas and Taras Fityo continues in a chat room here. \$\endgroup\$
    – DMGregory
    Mar 1, 2023 at 17:22
  • \$\begingroup\$ Thanks @DMGregory, that is very interesting! \$\endgroup\$ Mar 2, 2023 at 11:48


You must log in to answer this question.

Browse other questions tagged .