Search Issue Tracker

By Design

Votes

7

Found in

2022.3.19f1

2023.2.8f1

2023.3.0b5

6000.0.0b11

7000.0.0a1

Issue ID

UUM-63982

Regression

Yes

"InvalidCastException: Specified cast is not valid." error is thrown when scripts are compiled with PlayerBuildInterface.CompilePlayerScripts in Play Mode

-

Reproduction steps:
1. Open the “ReproProject“ project
2. Open the Preferences window (Edit > Preferences)
3. Set “Script Changes While Playing“ to “Recompile After Finished Playing“ in the General tab
4. Set “Auto Refresh” to “Enabled“ in the Asset Pipeline tab
5. Compile the scripts (ET > Compile)
6. Enter Play Mode
7. Open the “Assets/Scripts/HotfixView/Client/Demo/Opera/OperaComponentSystem“ script
8. Insert “Debug.Log(“123“);“ after line 19
9. Save the script
10. Repeat step 5
11. Exit Play Mode
12. Repeat step 7
13. Delete the “Debug.Log(“123“);“ line
14. Repeat step 9
15. Observe the Console window

Expected result: The scripts are recompiled and no errors are thrown in the Console window
Actual result: “InvalidCastException: Specified cast is not valid.“ error is thrown in the Console window

Reproducible with: 2022.2.0b16, 2022.3.19f1, 2023.2.8f1, 2023.3.0b5
Not reproducible with: 2021.3.34f1
Couldn’t test with: 2022.1.0a1, 2022.1.24f1, 2022.2.0a1, 2022.2.0b15 (due to RecompileAfterFinishedPlaying feature missing, compilation errors and UUM-14004)

Reproducible on: Windows 11 Pro (22H2)
Not reproducible on: No other environment tested

  1. Resolution Note:

    Untiy depends on having a UnitySynchronizationContext as its synchronization context whence configuring a custom one is not supported.

Comments (5)

  1. JackalLiu

    Jan 22, 2025 02:11

    这个问题稍微看一下UnityCSRef那个工程源码报错就能解决,那个报错函数第一行有个强转失败.

    var synchroContext = (UnitySynchronizationContext) SynchronizationContext.Current;

    伸手党可以把这个代码放在Unity.Core.dll

    using System.Threading; namespace ET { public class SynchronizationContextKeeper { #if UNITY_EDITOR private static SynchronizationContext UX; [UnityEditor.InitializeOnLoadMethod] static void Init() { UX = SynchronizationContext.Current; } #endif public static void BackToUX() { #if UNITY_EDITOR SynchronizationContext.SetSynchronizationContext(UX); #endif } } }

    然后在World.cs 的Dispose()中调用,在Dispose函数最后一行调用即可~

  2. xiaodu452981287

    Mar 08, 2024 03:31

    The complete error log is as follows:

    InvalidCastException: Specified cast is not valid.
    UnityEditor.Scripting.ScriptCompilation.EditorCompilation.CompleteActiveBuildWhilePumping ()

  3. xiaodu452981287

    Mar 08, 2024 02:22

    2022.3.20f1,Packaging bundle occasionally failed, this error occurred

  4. longsl

    Mar 01, 2024 07:19

    correct my comment information:> Reproduction in 2022.3.0f1

  5. longsl

    Mar 01, 2024 07:18

    Reproduction in 2023.3.0f1

Add comment

Log in to post comment

All about bugs

View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.