Domain Reloading resets your scripting state, and is enabled by default. It provides you with a completely fresh scripting state, and resets all static fields and registered handlers each time you enter Play Mode. This means each time you enter Play Mode in the Unity Editor, your Project begins playing in a very similar way to when it first starts up in a build.
Domain Reloading takes time, and this time increases with the number and complexity of the scriptsA piece of code that allows you to create your own Components, trigger game events, modify Component properties over time and respond to user input in any way you like. More info
See in Glossary in your Project. When it takes a long time to enter Play Mode, it becomes harder to rapidly iterate on your Project. This is the reason Unity provides the option to turn off Domain Reloading.
To disable Domain Reloading:
For more information, see How to configure Play Mode
When Domain Reloading is disabled, entering Play Mode is faster, because Unity does not reset the scripting state each time. However, it is then up to you to ensure your scripting state resets when you enter Play Mode. To do this, you need to add code that resets your scripting state when Play Mode starts.
When Domain Reloading is disabled, Unity still refreshes the scripting state when you update or re-import a script, based on your auto-refresh settings.
To ensure your scripting states correctly reset at Play Mode, you need to make adjustments to static fields and static event handlers in your scripts.
When Domain Reloading is disabled, the values of static fields in your code do not automatically reset to their original values. You need to add code that explicitly does this.
The following code example has a static counter field which increments when the user presses the Jump button. When Domain Reloading is enabled, the counter automatically resets to zero when entering Play Mode. When Domain Reloading is disabled, the counter does not reset; it keeps its value in and out of Play Mode. This means that on a second run of your Project in the Editor, the counter might not be at zero if it changed in the previous run.
using UnityEngine;
public class StaticCounterExample : MonoBehaviour
{
// this counter will not reset to zero when Domain Reloading is disabled
static int counter = 0;
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Jump"))
{
counter++;
Debug.Log("Counter: " + counter);
}
}
}
To make sure the counter resets even when Domain Reloading is disabled, you must use the [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)] attribute, and reset the value explicitly:
using UnityEngine;
public class StaticCounterExampleFixed : MonoBehaviour
{
static int counter = 0;
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
static void Init()
{
Debug.Log("Counter reset.");
counter = 0;
}
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Jump"))
{
counter++;
Debug.Log("Counter: " + counter);
}
}
}
With Domain Reloading disabled, Unity will not unregister methods from static event handlers when you exit Play Mode. This can lead to complications if you have code that registers methods with static event handlers. For example, on the first Play of your project in the editor, methods would be registered as normal. However on the second Play of your project, those methods would be registered a second time in addition to the first, causing those methods to be called twice when the event occurs.
For example, this code registers a method with the static event handler Application.quitting
With Domain Reloading enabled, Unity automatically resets the event handler when Play Mode starts, so the method is only ever registered once. However, with Domain Reloading disabled, the event handler is not cleared, so on the second run of your Project in the editor, the method is registered a second time, and is called twice when the event occurs - which is usually undesirable.
using UnityEngine;
public class StaticEventExample : MonoBehaviour
{
void Start()
{
Debug.Log("Registering quit function");
Application.quitting += Quit;
}
static void Quit()
{
Debug.Log("Quitting!");
}
}
When Domain Reloading is disabled, the above example adds the Quit
method again each time you enter Play Mode. This results in an additional “Quitting” message each time you exit Play Mode.
To ensure the event handler resets even when Domain Reloading is disabled, you must use the [RuntimeInitializeOnLoadMethod] attribute, and unregister the method explicitly so that it is not added twice.
using UnityEngine;
public class StaticEventExampleFixed : MonoBehaviour
{
[RuntimeInitializeOnLoadMethod]
static void RunOnStart()
{
Debug.Log("Unregistering quit function");
Application.quitting -= Quit;
}
void Start()
{
Debug.Log("Registering quit function");
Application.quitting += Quit;
}
static void Quit()
{
Debug.Log("Quitting the Player");
}
}
For runtime scripts, you must use the [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
attribute to reset static fields and event handlers.
For Editor scripts such as custom Editor windows or InspectorsA Unity window that displays information about the currently selected GameObject, asset or project settings, allowing you to inspect and edit the values. More info
See in Glossary that use statics, you must use the [InitializeOnEnterPlayMode]
attribute to reset static fields and event handlers.
When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.
More information
These cookies enable the website to provide enhanced functionality and personalisation. They may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
These cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising. Some 3rd party video providers do not allow video views without targeting cookies. If you are experiencing difficulty viewing a video, you will need to set your cookie preferences for targeting to yes if you wish to view videos from these providers. Unity does not control this.
These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.