Please allow us to recompile scripts automatically when entering playmode

This problem has already been described in several places:

I just want to make it an official feature request. To summarize, we have two options of how to recompile scripts, and neither are really acceptable:

  1. With auto-refresh on, every time we switch from the code editor to the Unity editor to check something we need to wait for the scripts to needlessly recompile before we can check it.
  2. With auto-refresh off, we need to remember to manually recompile the scripts every time we press play. If we don’t, things can get messy as we struggle to understand why our code isn’t working because there’s no indication that the scripts are out-of-date.

I believe option 2 is better because human error can be corrected, but the problem with option 1 isn’t a result of any error so Unity will never ā€œlearnā€ to stop wasting our time. However, when using option 2 I feel like there’s not really a good reason for Unity not to recompile scripts automatically when we press play, since this is a normal and expected function of development software. There is no case when a developer would want to enter playmode without using the up-to-date scripts.

To go along with this. It would also be great if we could disable automatic compiling too. Nothing is worse than when I save a file and it starts up a process that makes my CPU sound like it’s about to vtol off of an aircraft carrier and suddenly everything starts grinding to a halt for a few second while I’m still actively trying to type.

I have just recently disabled auto-refresh and wrote a small script that does an AssetDatabase.Refresh when exiting edit mode. Iā€˜ll share tze script when Iā€˜m back at my computer.

if modified scripts exist, this will cause a recompile, otherwise nothing happens

only issue being that if you launch Multiplayer Playmode the recompiled scripts will not take effect in the virtual players

Wait? Is this new? Does disabling auto-refresh actually apply for scripts in Unity6 now? I’d actually consider upgrading my project just for this alone.

It always has?

I’ve had it off as far back as Unity 2020 where scripts would only recompile when I specifically hit CRTL-R.

That hasn’t been my experience in the last year. It’ll work fine for assets and for code that hasn’t yet been picked up but not for code already in the project.

Though I’m wondering now if there isn’t some weird interaction between Visual Studio and Unity that is forcing Unity to recompile any time I save.

EDIT: Ah yep. There it is in the Unity Tools options of Visual Studio. I feel great knowing this now! But I also feel awful. This is why I can’t have nice things. Me.

It may also be confusing because there are cases where recompile still does occur. Mainly if some editor script or internally Refresh() gets called. Most notably if you make any change to an assembly definition.

Sorry, I was expecting people to follow the links for context. I’ve updated the post with a full explanation.

This is my script to auto-recompile when entering playmode (exiting edit mode actually) if auto-refresh is turned off:

using System;
using UnityEditor;
using UnityEngine;

namespace CodeSmileEditor
{
	/// <summary>
	///     If Preferences => Asset Pipeline => Auto Refresh is disabled, will call AssetDatabase.Refresh() before
	///     entering Playmode to ensure Playmode always uses the up-to-date versions of assets and scripts.
	/// </summary>
	/// <remarks>If no assets have changed this script will not slow down entering playmode.</remarks>
	internal static class RefreshAssetsOnEnterPlaymode
	{
		private const String AutoRefreshKey = "kAutoRefreshMode";
		private static Boolean IsAutoRefreshDisabled => EditorPrefs.GetInt(AutoRefreshKey, -1) == 0;

		[InitializeOnLoadMethod]
		private static void InitOnLoad()
		{
			EditorApplication.playModeStateChanged -= OnPlayModeStateChanged;
			EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
		}

		private static void OnPlayModeStateChanged(PlayModeStateChange state)
		{
			if (state == PlayModeStateChange.ExitingEditMode && IsAutoRefreshDisabled)
				AssetDatabase.Refresh();
		}
	}
}

It’s one of the cases where I condone the use of AssetDatabase.Refresh(). :wink:

Like I said, if you use Multiplayer Playmode and script compilation occurs, the Virtual Players’ scripts will be out of sync. But knowing that, it’s quick enough to hit stop even while compiling, then press play a second time.