Search Unity

Start method called recursively when creating new GameObjects

Discussion in 'Scripting' started by jazzbach, Jun 13, 2018.

  1. jazzbach

    jazzbach

    Joined:
    May 11, 2016
    Posts:
    41
    Hello !

    I'm following a series of Unity tutorials from an awesome site called "Cat Like Coding" and, there's an exercise where the author creates some instances of an Object so that their hierarchy looks like this:



    And the code is as follows (slightly modified by me):

    Code (CSharp):
    1. using UnityEngine;
    2. public class Fractal : MonoBehaviour {
    3.     public int maxDepth; // max number of instances to be created. Let's say 4
    4.     private int depth; // private counter
    5.    
    6.     private void Start() {
    7.         if (this.depth < this.maxDepth) {
    8.             new GameObject().AddComponent<Fractal>().MyInit(this); // Here is my question
    9.         }
    10.     }
    11.  
    12.     private void MyInit(Fractal parent){
    13.         this.maxDepth = parent.maxDepth;
    14.         this.depth = parent.depth + 1;
    15.         this.transform.parent = parent.transform;
    16.     }
    17. }
    What I understand is that, every time a new object is instantiated (in the middle of the game), the "Start" method (and "Awake" as well, I suppose) from all of its scripts will be called.

    Now, on the line "// Here is my question", I suppose that the order in which the methods are called is:
    • new GameObject() // Creates a new GameObject instance with no other component but a "Transform"
    • AddComponent<Fractal>() // Adds a component (in this case is a Script, which contains a "Start" method). Is the "Start" method of the newly created object called after the whole method chain finishes ??
    • MyInit(this); // Initializes the newly created object
    I'm just being curious about the order in which these methods are called because I think there's some kind of recursion implied in the process (Unity calling "Start" after "Start" and so on ...) Am I right ??

    The link of the tutorial is:
    https://catlikecoding.com/unity/tutorials/constructing-a-fractal/
     
  2. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    this tutorial looks kind of hard because the author writes three statements in one line which could be hard to follow for beginners.
    I would better write it like this:
    Code (csharp):
    1. GameObject go = new GameObject();
    2. Fractal frac = go.AddComponent<Fractal>();
    3. frac.MyInit(parent: this);
    however: you understood everything correctly so far :)

    regarding your question:
    There are several methods which are called by unity automatically.
    Awake()
    and
    OnEnable()
    are called immediately after creation.
    Start()
    is not called immediately. It is called right before the object is rendered the first time.
    Since they are created in a
    Start()
    Method themselves I would assume that their start method will be called all before the same frame -- so since there is not any other logic, in this case they are indeed called right after creation.

    here some information about execution order of unity methods: https://docs.unity3d.com/Manual/ExecutionOrder.html
     
    jazzbach likes this.
  3. jazzbach

    jazzbach

    Joined:
    May 11, 2016
    Posts:
    41
    Thank you very much for your explanation Hosnkobf. I'm getting used to method chaining because I see that pattern in plenty of tutorials.

    I've checked both, the link you made available about the execution order of event functions and your response and they've been super helpful. However, there are some tidbits that I still not understand. For example (in the posted code) if the "Start" method name is changed to "Awake", only one object gets created (instead of four). Why does this happen ?? Aren't all the objects still created in "Awake" method even though they are not rendered yet ? Maybe I'm delving too deep but this intrigues me :)