I apologize for my questions that may be stupid to you, but they confuse me very much…
First of all, on the top of the Start method in EnemyPathing script , there is a variable called waveConfig. Does this variable make WaveConfig script accessible in EnemyPathing script?
public class EnemyPathing : MonoBehaviour
{
WaveConfig waveConfig;
List<Transform> waypoints;
int waypointIndex = 0;
// Start is called before the first frame update
void Start()
{
waypoints = waveConfig.GetWaypoints();//get the transforms of waypoints
transform.position = waypoints[waypointIndex].transform.position;
Debug.Log("Waypoint: " + waypoints.Count);
}
If so, why don’t we need to say waveConfig = FindObjectOfType() just like what Rick did in the Block Breaker part?
Secondly, in the EnemySpawner script, what is the parameter “WaveConfig waveConfig” of the SpawnAllEnemiesInWave method for? Is it for connecting SpawnAllEnemiesInWave method with “currentWave” in the StartCoroutine method in Start(), so that it can access the Wave Scriptable object? If so, how does it do that?
// Start is called before the first frame update
void Start()
{
WaveConfig currentWave = waveConfigsList[startingWave];
StartCoroutine(SpawnAllEnemiesInWave(currentWave));
}
private IEnumerator SpawnAllEnemiesInWave(WaveConfig waveConfig)
{
for(int enemyCount = 0; enemyCount < waveConfig.GetNumberOfEnemies(); enemyCount++)
{
Instantiate(
waveConfig.GetEnemyPrefab(),
waveConfig.GetWaypoints()[0].transform.position,
Quaternion.identity);
yield return new WaitForSeconds(waveConfig.GetTimeBetweenSpawns());
}
}
Thirdly, in the EnemyPathing script, what does this.waveConfig = waveConfig change? I just do not understand what it does and why we have to do that…
public void SetWaveConfig(WaveConfig waveConfig)
{
this.waveConfig = waveConfig;
}
Fourthly, in the EnemySpawner script, when we say newEnemy.GetComponent().SetWaveConfig(waveConfig), why is it “waveConfig” within the Parentheses?
private IEnumerator SpawnAllEnemiesInWave(WaveConfig waveConfig)
{
for (int enemyCount = 0; enemyCount < waveConfig.GetNumberOfEnemies(); enemyCount++)
{
var newEnemy = Instantiate(
waveConfig.GetEnemyPrefab(),
waveConfig.GetWaypoints()[0].transform.position,
Quaternion.identity);
newEnemy.GetComponent<EnemyPathing>().SetWaveConfig(waveConfig);
yield return new WaitForSeconds(waveConfig.GetTimeBetweenSpawns());
}
}
Thanks a lot!