Lesson 108 +10 XP

Domain Reloading and Enter Play Mode

Domain Reloading and Enter Play Mode

Every time you enter Play Mode, Unity resets the scripting state and reloads the scene. You can optimize this process to save compilation and loading time.

Default Play Mode Sequence

By default, clicking Play triggers:

  1. Domain Reload: Resets the scripting domain, reinitializes static fields, and clears assemblies.
  2. Scene Reload: Destroys and recreates all GameObjects from the scene asset file.

Configurable Enter Play Mode Settings

To speed up iteration:

  1. Open Project Settings > Editor.
  2. Enable Enter Play Mode Settings.
  3. Uncheck Reload Domain and/or Reload Scene.

This makes entering Play Mode almost instant.

Handling Static Variables Without Domain Reload

If you disable Reload Domain, static variables will keep their values across play sessions:

public class ScoreManager : MonoBehaviour
{
    private static int _score = 0; // Does not reset to 0 when reloading!

    [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
    static void Init()
    {
        _score = 0; // Manually reset values when domain reload is disabled
    }
}

C# Compilation Preferences

Configure when compilation happens during play:

  • Go to Preferences > General > Script Changes While Playing:
  • Recompile and Continue Playing: Dynamically compiles code mid-play.
  • Recompile After Finished Playing: Safest; compiles only after you exit Play Mode.

TL;DR

  • Disable Reload Domain in Project Settings to enter Play Mode instantly.
  • Manually reset static fields using [RuntimeInitializeOnLoadMethod].
  • Configure compilation behaviors under general preferences.