How does this work

How does this work ?.

In the EnemySpawner ( function SpawnAllEnemiesInWave.) we create an instance of the enemy.

        var newEnemy = Instantiate(waveConfig.GetEnemyPrefab(), 
                                   waveConfig.GetWayPoints()[0].transform.position, 
                                   Quaternion.identity);

Afterwards we setup waveConfig for the enemy

        newEnemy.GetComponent<EnemyPathing>().SetWaveConfig(waveConfig);

But at first , when we first create the instance of the enemy, it does not know what the waveConfig is. It first know it when we set it up in the second line.

I can see that the start of EnemyPathing(the code below), when it create the instance of the Enemy, use waveConfig. But it is not set there at this time. Why does this work and not resulting in a error ?

private void Start()
{
    
    waypoints = waveConfig.GetWayPoints();

    transform.position = waypoints[waypointIndex].transform.position;

}

Mayby a constructor could be used here. Eg public void Start(WaveConfig waveConfig)

EnemySpawner has it’s own WaveConfig correct?
It’s using it’s own waveconfig to set up the enemy, then passes it to the enemy so it has it’s own reference to that particular waveconfig.

Hi Riley
Thanks to your respons !
When we at first create an instance of the Enemy there are only the EnemyPathing script attached to it. WaveConfig is not set at this time, but start() in EnemyPathin use it when it create the instance.
We first pass waveConfig in the second line from EnemySpawner to EnemyPathing.
Why do we not get an error when we create the instance ?

I’m assuming it’s because Start doesn’t get called immediately as soon as it’s instantiated try putting print statements right before you instantiate, in the start method and then right after the instantiation and see which order they appear in.

I don´t know how to test it. But if I turn off the second line : newEnemy.GetComponent().SetWaveConfig(waveConfig); I
immediately get the error I expected to get in the first line : NullReferenceException: Object reference not set to an instance of an object.
Strange, there must be some knowledge I´m missing. I think you are right that the Start() is not run immediately …

You could simply add a Debug.Log to all relevant methods and execute your code. The order of the messages in your console is the order in which the methods are getting called. Then you know immediately how Rick’s code works and why he doesn’t get a NullReferenceException. :slight_smile:

Okay, I have tried it out below.
SpawnAllEnemiesInWave create a Instance of the Enemy. Afterwards it goes to the next line and call the function SetWaveConfig in EnemyPathing . The function SetWaveConfig in EnemyPathing set up the waveConfig. First after this the Start function i EnemyPathing is activated . When it had run Start() it goes back to SpawnAllEnemiesInWave. And run the rest of the function.

So the Start function is first activatet when the script is called. But it first run the called function and then the Start function.

I did not expect that. I thought it run Start() when the Instance was created.

function SpawnAllEnemiesInWave, Before create Instance
function SpawnAllEnemiesInWave, After create Instance
function SetWaveConfig in EnemyPathing, Before this.waveConfig
function SpawnAllEnemiesInWave, After setting the waveConfig
function Start in EnemyPathing, Before Getting waypoints
function Start in EnemyPathing, After Getting waypoints
function SpawnAllEnemiesInWave, After yield return
function SpawnAllEnemiesInWave, Before create Instance

But are there no function that is run when you create an Instance ?