What is the best way to instantiate complex prefabs

Hey…
I’m having some problems when comes to instantiate a very complex prefab, with several objects.

I have tried that:
1 - If I keep all objects active in prefab and instantiate it, the game freeze for about 5 seconds, this is not very good xD
2 - If I keep some objects deactive in prefab and instantiate it, it goes very good, but I have to active the objects,
so I made a recursive function to active all objects that are deactive, it works much better, just with some lags but not freezed the game.

My intention with that is to make a infinite game in a circle shape, where I instantiate the top and bottom part of the level when needed.

If someone has a better solution to that will be welcome. :slight_smile:

here is the code that active the objects:

public void ActiveObjects(Transform parent, float timeActiveallObjects, int acceptableAmountChild)
    {
        StartCoroutine(activeObjects(parent, timeActiveallObjects, acceptableAmountChild));
    }
    IEnumerator activeObjects(Transform parent, float timeActiveAllObjects, int acceptableAmountChild)
    {
        int cont = 0;
        float waitTime = timeActiveAllObjects / parent.childCount;
        Transform child;
        
        // If the parent is deaticated, active it
        if (!parent.gameObject.activeSelf) parent.gameObject.SetActive(true);

        while (cont < parent.childCount)
        {
            child = parent.GetChild(cont);
            cont++;
            if (child.gameObject.activeSelf)
            {
                if (child.childCount > acceptableAmountChild)
                {
                    StartCoroutine(activeObjects(child, waitTime, acceptableAmountChild));
                }
            }
            else
            {
                child.gameObject.SetActive(true);

                if (child.childCount > acceptableAmountChild)
                {
                    StartCoroutine(activeObjects(child, waitTime, acceptableAmountChild));
                }
            }

            yield return new WaitForSeconds(waitTime);
        }
    }

How complex is “very complex”
If youve got thousands of objects just making up a single prefab, maybe you need to think about a better way of doing things