Attribute to clear static fields on play start


I made a lil tool that makes disabling domain reload easier. It's essentially two attributes.

Use on static fields, properties or events that you wish to reset on playmode. You can either "clear" the field (set the value to default), set it to a specified value, or make it assign itself a new instance of its type

Use on static methods that you want to execute during a domain reload. Basically shorthand for [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)], since that is obnoxiously long.

Here's a lil example. In this case, the instance field would be cleared, the event would be cleared, and the RunThis function would be invoked.

public class CharacterManager : MonoBehaviour
  //Will set value to default (null)
  static CharacterManager instance;

  //Works on events!
  static event Action onDoSomething;

  //Will set value to defined value (10)
  static int startsAsTen;

  //Will reset value, but make it not null (creates a new instance)
  static CharacterManager myNeverNullManager;

  static void RunThis()
      Debug.Log("Clean up here or something")

Here's the code! I sort of feel like something along these lines should be built into the engine, but it is what it is.

Thanks to @JGroxz for contributing some nice features!



1 Like

Nice utility, thanks for sharing!
As was mentioned I would like contribute to it by adding an accelerator TypeCache API. Let's see if we can make it very fast :)


Hey! First of all thank you for sharing this code - you have potentially saved thousands of seconds of play mode waits for me and other people who gonna use this :slight_smile:

While adding it to my project, I have made some additions to the code:

  • ClearOnReload can now take a default value as a parameter. The value will be assigned to the field/property on reload. Works for all built-in value types and strings (anything which can be expressed as a constant).
  • Same attribute now supports initializing fields with default instance of that field type. Very useful for static lists and arrays, as those will turn null otherwise and throw NullReferences if the code expects them to be initialized (which was the case for some of my scripts).
    These two things should leave even less reasons to write separate static methods just for the sake of variables initialization.

I created a pull request to your repo at GitHub. Please check it out and merge if you consider those changes helpful.



Amazing additions, merged!

I've been struggling to make it work on Singleton.
Turns out, I had to make variable/method public, to reset it
I guess, that's the way how it supposed to work in inherited classes

Thanks, it's quite helpful

Cool project, Josh! I took Alexey’s suggestion and forked the project to use TypeCache, and it improved performance significantly. Although it did restrict the clearable types to fields only. No properties or events. Pull request submitted.


Update merged! This does mean properties and events are no longer supported, but the huge jump in speed is super worth it in my eyes. If users do want to wipe properties, events, and other values, they can use the ExecuteOnReload attribute on a method that does so.

1 Like

We need to make this project Greate Again!

Please, update it to make support of generic types like Singleton : Monobehaviour. Also can it clear not only Monobehaviours but ScriptableObjects fields or even simple classes(not monobehaviours)?