Loading lessons...
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:
- Domain Reload: Resets the scripting domain, reinitializes static fields, and clears assemblies.
- Scene Reload: Destroys and recreates all GameObjects from the scene asset file.
Configurable Enter Play Mode Settings
To speed up iteration:
- Open Project Settings > Editor.
- Enable Enter Play Mode Settings.
- 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.