Occasionally, NetCodeConfig fails to load

In the editor, every so often the NetCodeConfig scriptable object fails to load. I’ve noticed it seems to occur more often after a fresh boot of Unity, but sometimes it occurs at other times too. Stopping and Playing again usually fixes it. When it fails to load, the singleton ClientServerTickRate is not present in neither the server nor the client world.

I’m not sure of the best place to store it, but I keep the scriptable in Resources.

Thanks for the report, Richay. Very odd.

  • Is it properly assigned in the ProjectSettings tab even when it errors?
  • Can you also log the value of NetcodeConfig.Global on entering playmode, please? And LMK if that’s null.
  • Are you using any AssetDatabase options that may impact this? Cache server, Source Control plugins etc?
  • Any error/warning logs?

I’ll do some testing on 1.2.0-pre.4.

I just added logging, and the very first time after a fresh boot of Unity, NetcodeConfig.Global is null. On a second play, all is good. The scriptable is still in the preload list in the player settings, and is linked in the netcode tab. We have no special AssetDatabase shenanigans going on afaik. Nothing in the console.

1 Like

I’m on dots 1.2.0-pre.6, Unity 2022.3.17f1.

It is probably because the first time you boot fresh (arguing eh) it is a classic egg/chicken problem. Something is not initialised in the right order and the config is not loading as expected.

Actually I tried to use NetcodeConfig.Global at authoring baker but it’s null when baking. Is that currently the known limitation? I would like official to support it since I dun want to put drag & drop this same NetcodeConfig to insane amount of authoring monobehavior.

I’ll take a look at the bug on Monday, but optimise I’m interested in why you’re adding the NetcodeConfig to any authoring components (nevermind multiple authorings).

If it’s set globally it auto applies.

1 Like

I’ve reproed this with 2022.3.13f1 (haven’t tried your editor version yet), thanks for the report! And apologies! It seems like Preloaded assets are not preloaded in the Editor (lol), which means Resources.FindObjectsOfTypeAll will fail. It’s an assumption I made after seeing it work. I’ll check out how other assets (like Graphics / Scriptable Rendering Pipeline) get around this.

Reproed by clearing local Library & Temp directories, fresh open of Unity, with the ProjectSettings window closed. Code:

        [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)]
// Inside NetCodeConfig.cs...
        private static void RuntimeTryFindSettings()
        {
            var configs = Resources.FindObjectsOfTypeAll<NetCodeConfig>();
            ...
            Global = configs.Length > 0 ? configs[0] : null;
            UnityEngine.Debug.LogError($"TEST RESULT: Global={Global} | {configs.Length}: {configs.Aggregate("", (c,n) => $"{c}\n{n}")}");
         }

My asset was in [project]/Assets/[here], so the Resources folder doesn’t seem to have an impact. Fix incoming…

3 Likes

After the fix, can I expect using NetcodeConfig.Global at authoring and it will work properly at authoring baking time?

This was recently fixed, but didn’t land in time for 1.2. Aiming for 1.3 instead. Thanks again for the report!

I’m not sure what you’re trying to do, optimize. What do you mean by “expect to use NetCodeConfig.Global at authoring time”? NetCodeConfig.Global is a runtime API.

2 Likes

I would like to use it at authoring time too but from wat u say it’s designed for runtime only. Then I will need to go for regular authoring way instead of just call NetCodeConfig.Global API to get the required NetCodeConfig data to bake data.

Yeah, I understood what you mean optimise. Indeed there should be a clear distinguish in between the authoring (the scriptable) and the actually config data (entity only) and the application of that data too (that is a third step).

@NikiWalker seeing as the fix wasn’t in pre.12, and I presume the next release won’t be for another month or two, is the fix straightforward to implement? It’s a regular irritance here, but I don’t want to restructure the net config initialization only to revert it again when the fix lands.

@Richay The fix is a little involved (as we moved the NetCodeConfig out of PreloadedAssets, except in builds), but in the editor, a short term quick solution is adding this code into an Editor script (like NetCodeConfigEditor):

        [InitializeOnLoadMethod]
        private static void InitializeNetCodeConfigEditorBugFix()
        {
            PlayerSettings.GetPreloadedAssets().OfType<NetCodeConfig>();
        }

Essentially you just have to ensure the ScriptableAsset is initialized in the editor (i.e. have it’s Awake called).

EDIT: And apologies that the fix didn’t land, it was too big of a change for 1.2. We’ll ensure it’s in the next minor release.

2 Likes

That works perfectly, many thanks. I wish I had asked a month ago!

1 Like

I experienced this same issue with Unity 6, using Netcode for entities 1.2.3 ( released on June 05, 2024 ).
The fix described in this thread worked wonders for me.

1 Like

For posterity: Fixed in Changelog | Netcode for Entities | 1.3.0-pre.4 .
* Issue where NetCodeConfig.Global did not load correctly on first boot, if not selected in the Project assets window. If you have a global NetCodeConfig set in your PreloadedAssets Project Setting, we'll also auto-upgrade your project, moving the save to Project Settings (via NetCodeClientAndServerSettings).

1 Like