Unity-Technologies/UnityCsReference · error · Exception

Calling GUISkin.current outside OnGUI. This might break the

Error message

Calling GUISkin.current outside OnGUI. This might break the styling of the Color Picker. Please do not access to Styles outside OnGUI.

What it means

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.

Source

Thrown at Editor/Mono/GUI/ColorPicker.cs:552

            static Styles()
            {
                var thumbSize = hueDialThumb.CalcSize(hueDialThumbFill);
                hueDialThumbSize = Mathf.Max(thumbSize.x, thumbSize.y);

                // Verify we're being called within OnGUI context to ensure proper style initialization.
                // The ColorPicker styles only exist in GUIUtility.GetDefaultSkin() (dark or light theme),
                // not in the minimal GameSkin from Editor Built-in Resources. The switch from GUISkin.current
                // to GUIUtility.GetDefaultSkin() happens elsewhere in the initialization flow.
                //
                // If this static constructor runs outside OnGUI, GUISkin.current won't be properly set yet,
                // causing style lookups to fail and breaking the Color Picker's layout.
                //
                // Instead of relying on the skin being correctly set up somewhere else, we enforce that
                // this Styles class is only initialized during OnGUI. This guarantees that when OnGUI
                // makes its first call to the Styles class, the default skin (dark or light) will be
                // available and all styles will initialize correctly.
                if (Event.current == null)
                    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.");
            }
        }

        public string currentPresetLibrary
        {
            get
            {
                InitializePresetsLibraryIfNeeded();
                return m_ColorLibraryEditor.currentLibraryWithoutExtension;
            }
            set
            {
                InitializePresetsLibraryIfNeeded();
                m_ColorLibraryEditor.currentLibraryWithoutExtension = value;
            }
        }

        void InitializePresetsLibraryIfNeeded()

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Move all ColorPicker/style access inside an OnGUI code path (within EditorGUI/GUILayout draw methods).
  2. Resolve styles lazily inside OnGUI rather than in static field initializers.
  3. Gate any GUI API call on Event.current != null before touching styles.

Example fix

// before
static readonly GUIStyle s = ColorPicker.Styles.someStyle;
// after
GUIStyle s;
void OnGUI()
{
    if (s == null) s = ColorPicker.Styles.someStyle;
}
Defensive patterns

Strategy: validation

Validate before calling

if (Event.current != null)
{
    // safe to touch ColorPicker / GUI styles here
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13). Data as JSON: /api/errors/f933714c36945b45. Report an issue: GitHub.