{"record":{"id":"f933714c36945b45","repo":"Unity-Technologies/UnityCsReference","slug":"calling-guiskin-current-outside-ongui-this-might","errorCode":null,"errorMessage":"Calling GUISkin.current outside OnGUI. This might break the styling of the Color Picker. Please do not access to Styles outside OnGUI.","messagePattern":"Calling GUISkin\\.current outside OnGUI\\. This might break the styling of the Color Picker\\. Please do not access to Styles outside OnGUI\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"Editor/Mono/GUI/ColorPicker.cs","lineNumber":552,"sourceCode":"            static Styles()\n            {\n                var thumbSize = hueDialThumb.CalcSize(hueDialThumbFill);\n                hueDialThumbSize = Mathf.Max(thumbSize.x, thumbSize.y);\n\n                // Verify we're being called within OnGUI context to ensure proper style initialization.\n                // The ColorPicker styles only exist in GUIUtility.GetDefaultSkin() (dark or light theme),\n                // not in the minimal GameSkin from Editor Built-in Resources. The switch from GUISkin.current\n                // to GUIUtility.GetDefaultSkin() happens elsewhere in the initialization flow.\n                //\n                // If this static constructor runs outside OnGUI, GUISkin.current won't be properly set yet,\n                // causing style lookups to fail and breaking the Color Picker's layout.\n                //\n                // Instead of relying on the skin being correctly set up somewhere else, we enforce that\n                // this Styles class is only initialized during OnGUI. This guarantees that when OnGUI\n                // makes its first call to the Styles class, the default skin (dark or light) will be\n                // available and all styles will initialize correctly.\n                if (Event.current == null)\n                    throw new Exception(\"Calling GUISkin.current outside OnGUI. This might break the styling of the Color Picker. Please do not access to Styles outside OnGUI.\");\n            }\n        }\n\n        public string currentPresetLibrary\n        {\n            get\n            {\n                InitializePresetsLibraryIfNeeded();\n                return m_ColorLibraryEditor.currentLibraryWithoutExtension;\n            }\n            set\n            {\n                InitializePresetsLibraryIfNeeded();\n                m_ColorLibraryEditor.currentLibraryWithoutExtension = value;\n            }\n        }\n\n        void InitializePresetsLibraryIfNeeded()","sourceCodeStart":534,"sourceCodeEnd":570,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/GUI/ColorPicker.cs#L534-L570","documentation":"ColorPicker's nested Styles class enforces that it is initialized only during an OnGUI pass: if Event.current is null (i.e., not inside OnGUI), it throws. The Color Picker depends on GUISkin.current being set, which only happens inside OnGUI, so initializing Styles outside it would break styling and layout.","triggerScenarios":"Touching ColorPicker or its styles from a non-GUI context: a static field initializer, EditorApplication.update, EditorApplication.delayCall, or a background thread; triggering the Styles static constructor before the first OnGUI.","commonSituations":"Referencing a ColorPicker style field in a PropertyDrawer/EditorWindow static initializer; calling GUI/ColorPicker helpers from update or delayCall; editor unit tests that run outside OnGUI.","solutions":["Move all ColorPicker/style access inside an OnGUI code path (within EditorGUI/GUILayout draw methods).","Resolve styles lazily inside OnGUI rather than in static field initializers.","Gate any GUI API call on Event.current != null before touching styles."],"exampleFix":"// before\nstatic readonly GUIStyle s = ColorPicker.Styles.someStyle;\n// after\nGUIStyle s;\nvoid OnGUI()\n{\n    if (s == null) s = ColorPicker.Styles.someStyle;\n}","handlingStrategy":"validation","validationCode":"if (Event.current != null)\n{\n    // safe to touch ColorPicker / GUI styles here\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never reference GUI styles in static field initializers; resolve them lazily inside OnGUI.","Gate any GUI/ColorPicker API call on Event.current != null.","Keep all style access inside OnGUI draw paths."],"tags":["imgui","ongui","color-picker","static-init","unity-editor"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}