Skip to main content
Cleanup
Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

Unity initializating Initializing prefabs with different scripts


I am creating a tower defense game.

I am creating a tower defense game.

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

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 it's attack radius, damage and so on.

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 TowerSO(scriptable object), it also contains a prefab for a tower.

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 spawnDelay, maximumUnitsToSpawn) andmaximumUnitsToSpawn, while the archer tower doesn't need this info, so.

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

Also 

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 some generic ifspecialized conditional logic for each type like (towerType == ArcherTower).
if (towerType == ArcherTower)

Currently my code looks like this:

 GameObject tower = Instantiate( currentlySelectedTowerSO.prefab,    new Vector3(gridPosition.x + xOffset, gridPosition.y + yOffset, currentlySelectedTowerSO.prefab.transform.position.z),   Quaternion.identity);  // initialize knighknight 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); 

So my question is, howHow do I separate different tower properties to different Scriptable Objects(with 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)
Thanks!?

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

  • For UnitTower(MaxUnitNumber, SpawnDelay)

Unity initializating prefabs with different scripts


I am creating a tower defense game.

Currently I have 2 different tower types 'Shooting tower', 'Unit tower'. I have a Scriptable object to initialize the tower, it contains some tower properties like it's attack radius, damage and so on.

I am initializing my towers from TowerSO(scriptable object), it also contains a prefab for a tower.

The problem here is that I have different properties for different tower types in TowerSO, for example unit tower has properties like(spawnDelay, maximumUnitsToSpawn) and archer tower doesn't need this info, so I guess it is not good approach because if there will be a new tower type with additional properties it would mean that TowerSO will grow more.

Also I want to be able to initialize tower when it's Instantiated but now I don't know what tower I am instantiating and I don't want to add some generic if like (towerType == ArcherTower).

 GameObject tower = Instantiate(currentlySelectedTowerSO.prefab,  new Vector3(gridPosition.x + xOffset, gridPosition.y + yOffset, currentlySelectedTowerSO.prefab.transform.position.z), Quaternion.identity);  // initialize knigh 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); 

So my question is, 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)
Thanks!

Initializing prefabs with different scripts

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( currentlySelectedTowerSO.prefab,   new Vector3(gridPosition.x + xOffset, gridPosition.y + yOffset, currentlySelectedTowerSO.prefab.transform.position.z),   Quaternion.identity); // 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)

Source Link

Unity initializating prefabs with different scripts


I am creating a tower defense game.

Currently I have 2 different tower types 'Shooting tower', 'Unit tower'. I have a Scriptable object to initialize the tower, it contains some tower properties like it's attack radius, damage and so on.

I am initializing my towers from TowerSO(scriptable object), it also contains a prefab for a tower.

The problem here is that I have different properties for different tower types in TowerSO, for example unit tower has properties like(spawnDelay, maximumUnitsToSpawn) and archer tower doesn't need this info, so I guess it is not good approach because if there will be a new tower type with additional properties it would mean that TowerSO will grow more.

Also I want to be able to initialize tower when it's Instantiated but now I don't know what tower I am instantiating and I don't want to add some generic if like (towerType == ArcherTower).

 GameObject tower = Instantiate(currentlySelectedTowerSO.prefab, new Vector3(gridPosition.x + xOffset, gridPosition.y + yOffset, currentlySelectedTowerSO.prefab.transform.position.z), Quaternion.identity); // initialize knigh 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); 

So my question is, 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)
Thanks!