When will the script of an object will start to run after instantiating it?


Here, is the highlisted code from the screenshot,

var enemy = Instantiate(wave.getEnemyPrefab(), wave.getEnemyWavePath()[0].transform.position, quaternion.identity);
 enemy.GetComponent<enemyPath>().setWave(wave);

My doubt is, When will the script of an object will start to run after instantiating it?
Here, I’m instantiating Enemy object with the Instantiate(wave.getEnemyPrefab(), wave.getEnemyWavePath()[0].transform.position, quaternion.identity);
As per my assumption, the script of the Enemy object will start to run immediately after instantiation, Right? But in my case the script only runs after calling setWave(wave) method which is in enemy Object script (setWave(wave) method will set this.wave = wave in enemy script) .

I tried adding more lines after the Instantiate(wave.getEnemyPrefab(), wave.getEnemyWavePath()[0].transform.position, quaternion.identity); line but it seems like the enemy script only runs after ***Yield return new WaitForSeconds() ;

You can find the full code in the above screenshot I’ve included.

My Assumptions After testing: The enemy script runs only when the scope in which it is present ends or to say simply enemy script runs only at the end of the scope in which is is present.
Is my assumption right?

Hi Allan,

Add Debug.Logs to the relevant methods and log Time.frameCount along with a meaningful message into your console. This way, you will be able to figure out in which order things are executed.

  1. You instantiate the enemy. Enemy Runs 1 - Awake(), 2- OnEnable(), 3 - Start(), 4 - Update().
  2. In start you instantiate enemy ant set its path passing a parameter wave.
  3. Enemy starts to follow path, while Enemy spawner waits for spawning another enemy.

I’m instantiating the enemy from another script . My doubt is when will the enemy script runs after instantiating it. The doubt arises for me because the enemy script seems to run only after the called block of code in which the enemy object instantiation is called, comes to end.
I’ve tested this by using debug.log statements. It seems that the enemy script gets executed only after the block of code in which it is called gets to an end.

How are you getting on with this Allan? Did you figure out what you wanted to know?

I figured that game development is not for me :slight_smile:

Allan don’t let this discourage you. It’s difficult when you work so long on a project and you don’t get the results you want. It feels like wasted time and effort. I would recommend putting this project on hold and try something else. You just need to change things up. Game development is a variety of different fields. Maybe try 3D modeling or try a different section. It will help you get out of this funk and feel better. Afterwards come back and see if you can solve this obstacle you ran into.

That’s really nice to hear kevin. I’ve learned some major concepts of game development 2d. It seems really boring for me after seeing the whole process of developing games which I was once excited about. Who knows maybe I’ll turn back to this game dev after some time, like you said I need some pause to get things right. Thanks Kevin for replying.

This topic was automatically closed after 5 days. New replies are no longer allowed.

Privacy & Terms