Skip to content

Fix RuntimeInitializeOnLoadMethod for generic singletons in Unity 6.3#191

Merged
wallstop merged 5 commits intomainfrom
copilot/fix-initialize-on-load-issue
Feb 7, 2026
Merged

Fix RuntimeInitializeOnLoadMethod for generic singletons in Unity 6.3#191
wallstop merged 5 commits intomainfrom
copilot/fix-initialize-on-load-issue

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 7, 2026

Description

Unity 6.3 rejects [RuntimeInitializeOnLoadMethod] in generic classes. Both RuntimeSingleton<T> and ScriptableObjectSingleton<T> used this attribute on their ClearInstance() methods, causing compilation failures.

Solution

Moved the attribute to non-generic registry classes that invoke registered clear actions:

  • RuntimeSingletonRegistry: Holds [RuntimeInitializeOnLoadMethod] and clears all RuntimeSingleton<T> instances
  • ScriptableObjectSingletonRegistry: Holds [RuntimeInitializeOnLoadMethod] and clears all ScriptableObjectSingleton<T> instances

Each generic singleton registers its ClearInstance method during static initialization:

public abstract class RuntimeSingleton<T> : MonoBehaviour where T : RuntimeSingleton<T>
{
    static RuntimeSingleton()
    {
        RuntimeSingletonRegistry.Register(ClearInstance);
    }
    
    internal static void ClearInstance() { /* ... */ }
}

The registry uses a HashSet<Action> for automatic deduplication and invokes all registered actions at runtime initialization:

internal static class RuntimeSingletonRegistry
{
    private static readonly HashSet<Action> _clearActions = new();

    [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
    private static void ClearAllInstances()
    {
        lock (_clearActions)
        {
            foreach (Action clearAction in _clearActions)
                clearAction.Invoke();
        }
    }
}

Thread-safe registration by locking directly on the collection. No reflection used.

Related Issue

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update
  • Refactor (code change that neither fixes a bug nor adds a feature)

Checklist

  • I have added tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have updated the CHANGELOG
  • My changes do not introduce breaking changes, or breaking changes are documented
Original prompt

This section details on the original issue you should resolve

<issue_title>[Bug]: InitializeOnLoad is Broken in Unity 6.3 (for Generic Classes)</issue_title>
<issue_description>### Description

ClearInstance in generic class — This is in the com.wallstop-studios.unity-helpers package (v3.1.7) in RuntimeSingleton and ScriptableObjectSingleton. Unity 6.3 now rejects [RuntimeInitializeOnLoadMethod] in generic classes. This requires a package update.

Steps to Reproduce

  1. Use Unity Helpers in a Unity 6.3 project

Expected Behavior

It works

Actual Behavior

It doesn't work

Unity Version

6000.x

Package Version

3.1.6

Platform

  • Editor
  • Windows
  • macOS
  • Linux
  • iOS
  • Android
  • WebGL
  • Other

Additional Context

No response</issue_description>

<agent_instructions>Figure out if we can keep the instance clearing, maybe by one more level of indirection. Ideally it is not done using reflection.</agent_instructions>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits February 7, 2026 18:09
Co-authored-by: wallstop <1045249+wallstop@users.noreply.github.com>
Co-authored-by: wallstop <1045249+wallstop@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix InitializeOnLoad method issue in Unity 6.3 Fix RuntimeInitializeOnLoadMethod for generic singletons in Unity 6.3 Feb 7, 2026
Copilot AI requested a review from wallstop February 7, 2026 18:11
Copy link
Copy Markdown
Owner

@wallstop wallstop left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Looks pretty good, left some minor feedback.

Comment thread Runtime/Utils/RuntimeSingletonRegistry.cs Outdated
Comment thread Runtime/Utils/RuntimeSingletonRegistry.cs
Comment thread Runtime/Utils/ScriptableObjectSingletonRegistry.cs
Co-authored-by: wallstop <1045249+wallstop@users.noreply.github.com>
Copilot AI requested a review from wallstop February 7, 2026 18:18
@wallstop wallstop marked this pull request as ready for review February 7, 2026 18:32
Copy link
Copy Markdown
Owner

@wallstop wallstop left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Need to make the header dates correct

Comment thread Runtime/Utils/RuntimeSingletonRegistry.cs Outdated
Co-authored-by: wallstop <1045249+wallstop@users.noreply.github.com>
Copilot AI requested a review from wallstop February 7, 2026 18:56
@wallstop wallstop merged commit b5928b9 into main Feb 7, 2026
18 checks passed
@wallstop wallstop deleted the copilot/fix-initialize-on-load-issue branch February 7, 2026 18:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: InitializeOnLoad is Broken in Unity 6.3 (for Generic Classes)

2 participants