I have three scritps and one plain C# class.
EnemyMonobehaviour, EnemyAttackMonobehaviour and UnitState plain class.
In enemyMono I have Initialize function that initializes unitState.
public void Initialize(float moveSpeed, Transform[] waypoints,
Damage damage, Armor armor, Health health, float magicalResistance, int bounty) {
this.unitState = new UnitState(damage, armor, health, magicalResistance);
this.bounty = bounty;
GetComponent<EnemyFollowPath>().Initialize(moveSpeed, waypoints);
}
I have a WaveManager class that spawns different enemies and than later it initializes them.
GameObject enemyUnit = container.InstantiatePrefab(enemy.prefab, spawningPoint, Quaternion.identity, null);
enemyUnit.GetComponent<Enemy>().Initialize(2f, EnemyPath.Instance.getWaypoints(),
new Damage(enemy.damageType, enemy.attackType, enemy.minDamage, enemy.maxDamage),
new Armor(enemy.armorType, enemy.armor),
new Health(enemy.maxHealth), 10, enemy.bounty);
And now here is where the problem appears. EnemyAttack is initialized like this
private void Awake() {
this.enemy = GetComponent<Enemy>();
IUnitState unitState = enemy.GetState();
minDamage = unitState.GetDamage().minDamage;
maxDamage = unitState.GetDamage().maxDamage;
damageType = unitState.GetDamage().damageType;
attackType = unitState.GetDamage().attackType;
}
EnemyAttack.Awake() is being called before the Initialize
function from the Enemy script while unit state is still null
. So the question is how to correctly initialize monobehaviour component field values if there are some fields that cannot be passed as SerializeFields or Prefabs in Editor and need to be passed down during Instantiation?
Also I don't want to initialize EnemyAttack From Enemy.Initialize function. My initial approach was to have EnemyMono that contains various properties that other scripts can use like EnemyAttack, EnemyFollowPath and so on. They just need to acquire Enemy reference with GetComponent();