static readonly - is reset without Domain Reload

A static readonly variable like Unity.Physics.Material returns default inside a Burst compiled job the next plays after the first.

Reproduction:

class T
{
        //Unity.Physics.Material copy
        public struct TestStruct
        {
            public CombinePolicy FrictionCombinePolicy;
            public CombinePolicy RestitutionCombinePolicy;
            public byte CustomTags;
            public float Friction;
            public float Restitution;
            public CollisionResponsePolicy CollisionResponse { get; set; }
            public bool EnableMassFactors { get; set; }
            public bool EnableSurfaceVelocity { get; set; }
        }
 
        public static readonly TestStruct test = new TestStruct() { Friction = 1 };

        [BurstCompile]
        public struct TestJob : IJob
        {
            public void Execute()
            {
                Debug.Log($"{test.Friction}");
            }
        }

        [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
        static void SubsystemRegistration()
        {
            // Just disabling Domain Reload (this part is not tested)
            if (!EditorSettings.enterPlayModeOptionsEnabled)
            {
                EditorSettings.enterPlayModeOptionsEnabled = true;
                EditorSettings.enterPlayModeOptions = EnterPlayModeOptions.DisableDomainReload | EnterPlayModeOptions.DisableSceneReload;
            }

            var q = new TestJob().Schedule();
            q.Complete();
            EditorApplication.isPlaying = false;
            return;
        }
}

Problem found here .
@ Burst 1.4.4

1 Like

Just to rule it out - could you set [BurstCompile(CompileSynchronously =t rue)] on that job and tell us the result please?

I’m suspicious that you are running the managed version of the job on the first iteration, and then the Burst job on the second iteration (thus the differing result!).

Yes, that shows that the problem is not related to DomainReload.

        public struct TestStruct
        {
            public float CustomTags;
            public float Friction;
        }

        public static readonly TestStruct test = new TestStruct() { Friction = 1 };

        [Unity.Burst.BurstCompile(CompileSynchronously = true)]
        public struct TestJob : IJob
        {
            public void Execute()
            {
                UnityEngine.Debug.Log($"{test.Friction}, {test.CustomTags}");
            }
        }

Will always print “0, 1”, but it should be “1, 0”

Ok cool, I can reproduce this. We’ve closed off 1.4 now, but I’ll get a fix for this landed in 1.5!

Oh and as a workaround for now, if you explicitly set CustomTags = 0, it’ll work as expected until we can get a bugfix landed!

1 Like