Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Delaying the Start function of an entire GameObject's hierarchy

Discussion in 'Scripting' started by jdruzdzel, Sep 1, 2022.

  1. jdruzdzel

    jdruzdzel

    Joined:
    Jan 9, 2018
    Posts:
    8
    Hello,

    I am creating a GameObject hierarchy over many frames, with many Components on various children GameObjects.
    I want to delay initialization until the whole hierarchy is created, however simply disabling the root GameObject isn't preventing newly created Components on its children from calling Start. Is the only way to postpone Start by disabling every GameObject with a script?

    Cheers
     
  2. jdruzdzel

    jdruzdzel

    Joined:
    Jan 9, 2018
    Posts:
    8
    I realize disabling the root object *does* indeed halt Start for its entire hierarchy; my scripts had errors. Yay
     
  3. Obscure045

    Obscure045

    Joined:
    Aug 2, 2022
    Posts:
    14
    Hello,
    Here is my code it is working correctly.
    Code (CSharp):
    1.     bool initialized = false;
    2.  
    3.     // Update is called once per frame
    4.     void Update()
    5.     {
    6.         if (this.transform.parent.gameObject.activeSelf)
    7.         {
    8.             FunctionCalledByButton();
    9.         }
    10.                 if (this.transform.parent.gameObject.activeSelf && !initialized)
    11.         {
    12.             InitializeFunction();
    13.             initialized = true;
    14.         }
    15.     }
    16.  
    17.     void FunctionCalledByButton()
    18.     {
    19.         Debug.Log("Function being called while the parent is active.");
    20.     }
    21.  
    22.     void InitializeFunction()
    23.     {
    24.         Debug.Log("Function called only one time.");
    25.     }
    Explanation:
    On the first line, we set a private variable to false.
    Using this variable we can check if to call the initialize method on the continuous method.
    And the rest is clear as glass.
     
    jdruzdzel likes this.
  4. Obscure045

    Obscure045

    Joined:
    Aug 2, 2022
    Posts:
    14
    A bit of a formatting error, but you get it. RIGHT.
     
    jdruzdzel likes this.
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,015
    A much better way would be to introduce your own 'Initialise' method, probably composed with an interface that you implement in any components that requires it.

    As you generate all these game objects you can keep references to them in a collection. Once you're done, then you can run down the collection and initialise them.

    Code (CSharp):
    1. public interface IInitialisable
    2. {
    3.     public void Initialise();
    4. }
    5.  
    6. //usage
    7. foreach (GameObject go in gameObjectsIMade)
    8. {
    9.     if (go.TryGetComponent(out IInitialisable initialise)
    10.     {
    11.         initialise.Initialise();
    12.     }
    13. }
     
    jdruzdzel likes this.