How to wait that a GameObject instance has completed own Start from another GameObject instance's Start with Unity?

1146 views unity3d
9

I have two GameObjects like this:

public class GOA : MonoBehaviour
{
    void Start()
    {
     ... do something ...
    }
}

and another object that depends from the first in this way:

public class GOB : MonoBehaviour
{
    void Start()
    { 
     // wait GOA has terminated own "Start" life cycle
     ... then do something ... 
    }
}

How can I make GOB:Start() to wait until GOA:Start() has terminated?

answered question

1 Answer

12

I already had to do that thing, cascade initialisation. The trick I found was this one :

public class GOA : MonoBehaviour
{
    public bool isInitialized = false;

    void Start()
    {
        // ... do something ...
        isInitialized = true;
    }
}

And then, in class B, I initialize in Update() method. I'm using a boolean to make sure the initialisation will occur only once

public class GOB : MonoBehaviour
{
    public bool isInitialized = false;
    var a = GameObject.Find("A");

    void Update() //Update ! Not Start()
    {
        if (!isInitialized  && a.GetComponent<GOA>().isInitialized)
        {
            // initialize B...
            isInitialized = true;
        }
    }
}

posted this

Have an answer?

JD

Please login first before posting an answer.

Ads

Categories