Bug with loading scene from asset bundle

Hi there :slight_smile:

I am experiencing a weird bug when loading a scene from an asset bundle -
for some bizarre reason, the GameObjects within the scene are reorganized.
Specifically - they are sorted alphabetically.

For example, I am building a simple mostly empty scene with the following hierarchy:

However, when loading that scene from the asset bundle, it looks like this:

The following is my asset bundle building logic:

[MenuItem("Assets/Build AssetBundles")]
    private static void BuildAllAssetBundles()
    {
        string bundleName = "emptyScene";
        string timestamp = DateTime.Now.ToShortTimeString().Replace(":","");
       
        string assetBundleDirectory = "Assets/StreamingAssets";
        if (!Directory.Exists(Application.streamingAssetsPath))
            Directory.CreateDirectory(assetBundleDirectory);

        AssetBundleBuild[] buildMap = new AssetBundleBuild[1];
        buildMap[0].assetBundleName = bundleName + "_" + timestamp;
        buildMap[0].assetNames = new string[1]
        {
            "Assets/Scenes/EmptyScene.unity"
        };
       
        BuildPipeline.BuildAssetBundles(assetBundleDirectory, buildMap, BuildAssetBundleOptions.None,
            EditorUserBuildSettings.activeBuildTarget);
    }

Has anyone experienced this bug before? Perhaps a clue as to what may be the cause of the issue?

Thanks in advance!

I don’t think the order of root objects is defined. It’s just sort of a big bag-o-data. Watching a running game it sorta seems random for the most part.

Everything that is parented under another GameObject should still be correctly ordered.

Hi Kurt-Dekker,

This isn’t a big deal if it comes to random GameObjects, however in the world of UI the order of children directly affects how the objects are rendered on top of each other.

Did you read what I wrote?

In my “world of UI” everything lives under a Canvas.

Therefore the items below it are parented.

Therefore its order is preserved.

Is that not the case?

Oh, you’re correct. My bad.
Not only is the parent-child relationship preserved, but all non-root GameObjects maintain their child index.

Thanks for the help!

1 Like