Search Unity

Bug (Case 1426900) Error "Destroy may not be called from edit mode!" is shown when stopping play

Discussion in 'UI Toolkit' started by Kichang-Kim, May 11, 2022.

  1. Kichang-Kim

    Kichang-Kim

    Joined:
    Oct 19, 2010
    Posts:
    1,012
    Hi. I found that when I stop play mode in editor, an error "Destroy may not be called from edit mode!" is shown in the console. This only occurred that the scene has UIDocument and InputSystemUIInputModule objects. Tested on Unity 2021.3.2f1.

    Here is full error message:
    Reported as Case 1426900.
     
  2. Teemu_T

    Teemu_T

    Joined:
    Jan 5, 2022
    Posts:
    2
    We have the same issue with this editor version. We're loading a HUD scene asynchronously, and leaving it off will get rid of the errors, even if the original scene has UI elements. Running this HUD scene alone will cause a similar error to appear for every UI document in the scene on load:

    It seems like the error has something to do with the UI Documents using different panel settings. These settings have different sort orders, but are identical otherwise. If all the document components are disabled, scene is ran, and then only one enabled, the error goes away. Enabling more than one will cause the error to return.

    After testing some things while typing this, the errors that appeared on HUD scene load aren't appearing anymore, no idea why. I also noticed that just compiling scripts without the game running will throw these errors, too.
     
    gareth_untether likes this.
  3. RunninglVlan

    RunninglVlan

    Joined:
    Nov 6, 2018
    Posts:
    182
    I have the same error and don't know if I'm responsible, because there's no StackTrace
    I'm on 2022.1.0f1
     
  4. RunninglVlan

    RunninglVlan

    Joined:
    Nov 6, 2018
    Posts:
    182
    Kichang-Kim, did Unity QA already provide some comments on your report?
     
  5. Kichang-Kim

    Kichang-Kim

    Joined:
    Oct 19, 2010
    Posts:
    1,012
    No, Unity did not send any response to me yet.
     
  6. Kichang-Kim

    Kichang-Kim

    Joined:
    Oct 19, 2010
    Posts:
    1,012
  7. softleitner

    softleitner

    Joined:
    Jan 10, 2021
    Posts:
    44
    anything new about that error?

    the issues has been marked as duplicate without any link to the actual issue....

    i get the error whenever i press play in a fresh 2021.3.4 project if i use both the ui toolkit and the new input system
     
  8. BillyWM

    BillyWM

    Joined:
    Dec 29, 2018
    Posts:
    14
    That's really irritating that they leave it to us to hunt down the original, because knowing the way Unity does things, this could be a bug that's been around since 1974
     
    chrysu76 likes this.
  9. uMathieu

    uMathieu

    Unity Technologies

    Joined:
    Jun 6, 2017
    Posts:
    398
    The original issue was logged from our internal bug reporting system. A fix is coming soon to 2022.2, it will be then backported to previous versions.
     
  10. oli-2019

    oli-2019

    Joined:
    Aug 2, 2019
    Posts:
    4
    Having the same error in 2021.3.2f1.

    since the ticket 1426900 is marked as duplicate it's quite challenging to know when this is fixed. will the "duplicate" be changed to fixed, when an fix is released? or will it stay on "duplicate"?

    also, shouldn't the issue be mentioned somewhere in "known issues" at https://unity3d.com/unity/whats-new/2021.3.2 (or in the later versions, if it's not fixed there already)?


    also, mentioning a release date for the versions somewhere on those pages would be nice. trying to find out if any of these versions was released since the last post on this thread was made, to somehow find out if there is a version that COULD contain the fix (without searching through all the versions and their release notes).
     
    SBods9A likes this.
  11. bugcamper

    bugcamper

    Joined:
    Mar 15, 2022
    Posts:
    11
    has anyone found a hack to get around this error? it's not acceptable for my project to have error displayed while waiting for this fix.
     
  12. WidmerNoel

    WidmerNoel

    Joined:
    Jun 3, 2014
    Posts:
    66
    @uMathieu Could you update us once a fix is backported? I am also having this issue with the 2021 LTS (2021.3.6f1). Do you backport such issues to all LTS?
     
    garrettmmoore likes this.
  13. burningmime

    burningmime

    Joined:
    Jan 25, 2014
    Posts:
    845
    Isn't the error only displayed on the editor console? This probably wouldn't matter much for local development, although i can see if you're a contractor and ship that to a client it could take a bit of explaining.
     
    RunninglVlan likes this.
  14. FaithlessOne

    FaithlessOne

    Joined:
    Jun 19, 2017
    Posts:
    324
    I don't know whether or not the stated error also shows up in the builded game, but game developers may find it undesirable to permanently have errors in the editor console during development. Reason is: You are likely going to ignore errors showing up in the console, because there are always these stated errors and may miss other errors completely unrelated to the stated ones. You are also not able to fix the stated error, because it is a Unity internal bug. So I also prefer this error to be fixed by Unity team to have a clean view in the editor console of the real errors happened.
     
  15. WidmerNoel

    WidmerNoel

    Joined:
    Jun 3, 2014
    Posts:
    66
    If we start accepting this kind of mentality, which results in mediocre software, we might as well ship games that crash every couple hours. Players can also simply restart the game when it crashes right? As long as it doesn't happen too often. And in the end these are just games. Nothing serious. So who cares anyway.

    I am using an LTS version. How can such an error, one that is reproducible every time when you hit play, enter an LTS release build? There should be alarms going off at Unity HQ for something like this. And they should go off before the new LTS is shiped. I don't understand how this can make it's way into an LTS release.

    This kind of mentality (which has been demonstrated and lived by Unity for many years now) is way beyond my expectations towards quality software and I can identify with that approach less and less every week. I wonder how long Unity can hold their stagnant advantage over other software products up.

    Great talk on the topic (60min): Jonathan Blow - Preventing the Collapse of Civilization
     
  16. FaithlessOne

    FaithlessOne

    Joined:
    Jun 19, 2017
    Posts:
    324
    I fully agree. But we can be glad, the error seems to be fixed at least in the most recent LTS release of Unity 2021.3.7.
     
    WidmerNoel likes this.
  17. WidmerNoel

    WidmerNoel

    Joined:
    Jun 3, 2014
    Posts:
    66
    Happy to see that the new LTS has been shipped right after my rant while I was asleep :D Thank you for the info. I haven't tested the fix yet though. EDIT: Tested it now and it is not fixed :(
     
    Last edited: Jul 28, 2022
    MontblackJJ and SBods9A like this.
  18. FaithlessOne

    FaithlessOne

    Joined:
    Jun 19, 2017
    Posts:
    324
    Hmm, sorry, error still occurs. I am not sure if the error now occurs more rarely but definitely is there.
     
  19. georgeneversleep

    georgeneversleep

    Joined:
    Jun 4, 2016
    Posts:
    2
    Neither 2021.3.7 nor 2021.3.8f1 fix the problem. It's still there.
     
  20. nomiswerdnek

    nomiswerdnek

    Joined:
    Jul 14, 2017
    Posts:
    6
    Agreed, I encountered this issue a few versions ago, and it's still present in 2021.3.8f1
     
  21. VOTRUBEC

    VOTRUBEC

    Joined:
    Dec 17, 2014
    Posts:
    106
    Just an FYI - 2022.1.14 and error still present.
     
  22. curbol

    curbol

    Joined:
    Oct 29, 2015
    Posts:
    8
    I'm also getting this error. Is this still being worked on? Seems that it has been quite a long time since the original post and it's still not resolved.
     
  23. better_walk_away

    better_walk_away

    Joined:
    Jul 12, 2016
    Posts:
    291
    I have a slightly different error:
    Code (CSharp):
    1. Destroy may not be called from edit mode! Use DestroyImmediate instead.
    2. Destroying an object in edit mode destroys it permanently.
    3. UnityEngine.UIElements.PanelSettings:OnDisable ()
    I am using Unity 2021.3.8f1.
     
  24. mikejm_

    mikejm_

    Joined:
    Oct 9, 2021
    Posts:
    346
    Glad I saw this thread. I've been getting this for ages but I assumed it was something I did wrong. :D

    Anyway I'm happy to ignore it for now. Doesn't really hurt anything except cause a bit of clutter as noted.

    I tried switching to 2022.2 when it came out but that version has a major problem with the VideoPlayer which I use so I can't function with that yet:

    https://forum.unity.com/threads/vid...-usage-videoplayer-completely-broken.1323147/
     
  25. WF_Bart

    WF_Bart

    Joined:
    Apr 16, 2014
    Posts:
    29
    I not only have this error, but it causes builds to fail like 50% of the time. It's really annoying, please do something about it.
     
  26. nomiswerdnek

    nomiswerdnek

    Joined:
    Jul 14, 2017
    Posts:
    6
    Same thing for me. A second build attempt always works after a first failed attempt, but very annoying.
     
  27. FaithlessOne

    FaithlessOne

    Joined:
    Jun 19, 2017
    Posts:
    324
    I switch to an empty scene (which is also not included in the build) in my project without any UIToolkit stuff in it (I suppose thats the important thing) and then build the project. I never got the stated error then and the build works correctly at least in Unity 2021.3. Maybe it will also work in your case.
     
    RunninglVlan likes this.
  28. Gamebient

    Gamebient

    Joined:
    Mar 8, 2019
    Posts:
    15
    I FOUND THE PROBLEM - UNITY, PLEASE FIX!

    Hey everybody, I too had/have this issue.
    I looked through the code and found the issue. Please unity dev team, fix this NOW - this problem has been known for months and I found a solution in just a few minutes!!
    It seems like the PanelSettings and UIElementsRuntimeUtility have calls to the EventSystem that trigger the error when the editorstate changes from editmode to playmode or vice versa. The error happens in line 385 inside the CreateUIToolkitPanelGameObject method of the EventSystem.
    Code (CSharp):
    1. #if PACKAGE_UITOOLKIT
    2. private void CreateUIToolkitPanelGameObject(BaseRuntimePanel panel)
    3. {
    4.     if (panel.selectableGameObject == null)
    5.     {
    6.         var go = new GameObject(panel.name, typeof(PanelEventHandler), typeof(PanelRaycaster));
    7.         go.transform.SetParent(transform);
    8.         panel.selectableGameObject = go;
    9.         panel.destroyed += () => Destroy(go); // The error happens here
    10.     }
    11. }
    12.  
    13. #endif
    What fixes the issue is changing
    panel.destroyed += () => Destroy(go);
    to:
    Code (CSharp):
    1. panel.destroyed += () =>
    2. {
    3.     if (Application.isPlaying)
    4.         Destroy(go);
    5.     else
    6.         DestroyImmediate(go);
    7. };
    A simple check whether we are in playmode or not fixes the issue once and for all.
    So PLEASE, forward this to a developer to fix! It literally takes only a minute and does not break compatibility, as all versions supporting the UIToolkit (now UIElements) package won't need further changes for this to work.
     
    Last edited: Sep 21, 2022
    detzt, bitbebop, wansy and 7 others like this.
  29. StripeGuy

    StripeGuy

    Joined:
    Dec 30, 2016
    Posts:
    52
    EDIT: Nevermind. It worked! I just edited it in Atom at first (laziness). But once I edited it in VisualStudio, it worked.

    Great find and great timing on my part searching this a few minutes after you posted it.

    So I did a search for that line of code and found it in:
    Library\PackageCache\com.unity.ugui@1.0.0\Runtime\EventSystem\EventSystem.cs

    The problem is that after I change the script to what you suggest and go back to unity, it changes the script right back to the original. So how are you supposed to save it?
     
  30. Gamebient

    Gamebient

    Joined:
    Mar 8, 2019
    Posts:
    15
    You can't and shouldn't - in theory. That's the way it is with unity internal code. The only way I was able to verify that it works is by copying the contents of Library\PackageCache\com.unity.ugui@1.0.0 to somewhere else, removing Unity UI in the package manager and copying the backup of the folder to somewhere inside the asset folder.
    This is not advisable for a few reasons:
    - First, as far as I know, you are not allowed to copy and/or modify any of unity's internal code.
    - You won't get further updates for this package
    - Other packages will break (example: InputSystem - InputSystemUIInputModule supposedly "missing") or try to install Unity UI again, because they depend on it (example: com.unity.visualscripting, com.unity.render-pipelines.core)
    Conclusion: Therefore we have no other choice than to wait for the unity dev team to do the changes for us
     
  31. DavidNLN

    DavidNLN

    Joined:
    Sep 27, 2018
    Posts:
    90
    As noted above this it still not fixed in 2022.2 is there still an open issue for this?
     
    jjbish likes this.
  32. lowercasename

    lowercasename

    Joined:
    Mar 4, 2022
    Posts:
    2
    It would be really great if this were properly addressed, we're encountering this constantly too in 2021.3.9 and it consistently causes builds to fail.
     
    jjbish likes this.
  33. Strom_CL

    Strom_CL

    Joined:
    Nov 17, 2012
    Posts:
    115
    Running into this today, looks like an easy fix, Unity fix this, simple error, simple fix, no reason not to be in the next release of every version.
     
  34. gencontain

    gencontain

    Joined:
    Nov 15, 2012
    Posts:
    15
    I think this got fixed in 2022.1.19:

    • UI Toolkit: Fixed EventSystem error logged in Editor when entering and leaving Play mode if there's a UIDocument in the scene. (UUM-10360)
     
  35. Strom_CL

    Strom_CL

    Joined:
    Nov 17, 2012
    Posts:
    115
    Nice, now we just need them to roll this back into 2021.3.x, this issue still persists in 2021.3.11f1 LTS.
     
  36. Gamebient

    Gamebient

    Joined:
    Mar 8, 2019
    Posts:
    15
    Well, at least they found the bug now. Still, as @Strom_CL mentioned, this should be backported. 2021 was very unstable for quite some time and I don't want to experience the same with 2022. Furthermore, 2021 is the latest LTS version, meaning it should get quality and bug-fixing updates. As this was clearly a bug, there is no reason to force developers making a switch to the next version for something this simple.
     
    Strom_CL likes this.
  37. johfirefly

    johfirefly

    Joined:
    Aug 21, 2018
    Posts:
    15
    Such indifference to the bugs - affecting all users of the new UI - seems to put me off it :\
     
  38. Strom_CL

    Strom_CL

    Joined:
    Nov 17, 2012
    Posts:
    115
    Looks like this was fixed in 2021.3.12f1:

    upload_2022-10-26_20-23-11.png
     
    canklot likes this.
  39. Gamebient

    Gamebient

    Joined:
    Mar 8, 2019
    Posts:
    15
    Yup, just saw it.
    I guess we can use UIElements outside the editor without concerns now..?
     
  40. Suzie-QQ

    Suzie-QQ

    Joined:
    Aug 21, 2019
    Posts:
    1
    Has anyone found the 'duplicate' ticket that corresponds to 11426900? https://issuetracker.unity3d.com/is...e-instead-dot-is-shown-when-exiting-play-mode

    This definitely is not fixed yet and I agree 100% with Gamebient that the source of the issue(s) is in the EventSystem.cs class where the #if PACKAGE_UITOOLKIT logic is. These "Send Message" warnings are pretty annoying.

    I am guessing no one knows how to turn them off or has a work around. Yes - my code works fine but the Editor warnings are annoying.
     
  41. Devseba

    Devseba

    Joined:
    Feb 26, 2021
    Posts:
    9

    Destroy may not be called from edit mode! Use DestroyImmediate instead.
    Destroying an object in edit mode destroys it permanently.
    UnityEngine.UIElements.PanelSettings:OnDisable ()


    Same error using 2023.2.0b14

    Edit: Same error using 2023.2.0b15
    Edit2: Same error using 2023.2.0b16
    Edit3: Same error using 2023.2.0b17
     
    Last edited: Nov 9, 2023
    daxiongmao likes this.
  42. Mj-Kkaya

    Mj-Kkaya

    Joined:
    Oct 10, 2017
    Posts:
    179
    I have it too.
     
    daxiongmao likes this.
  43. johfirefly

    johfirefly

    Joined:
    Aug 21, 2018
    Posts:
    15
    I have it too :mad:
     
    daxiongmao likes this.
  44. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,935
    @uMathieu or anyone else, could we get a word on this at all?

    It should be a simple fix as posted above.
     
  45. uMathieu

    uMathieu

    Unity Technologies

    Joined:
    Jun 6, 2017
    Posts:
    398
    Could it be that somehow the PackageManager cache wasn't properly updated and the ugui code is still an old version?
     
  46. Tron_314159265359

    Tron_314159265359

    Joined:
    Apr 27, 2020
    Posts:
    6
    Navigate to the package cache folder corresponding to the version of the Input System used in your project:
    <your-project-path>\Library\PackageCache\com.unity.inputsystem@1.7.0\InputSystem\Plugins\InputForUI\

    Open the file InputSystemProvider.cs and navigate to line 645:
    Code (CSharp):
    1. UnityEngine.Object.Destroy(m_InputActionAsset); // TODO check if this is ok

    Change it into:
    Code (CSharp):
    1. UnityEngine.Object.DestroyImmediate(m_InputActionAsset); // Now it is ok!
     
    Last edited: Nov 18, 2023
  47. Mj-Kkaya

    Mj-Kkaya

    Joined:
    Oct 10, 2017
    Posts:
    179
    Wow!!! It is working Unity 2023.2.0f1
    Thanks a lot!
     
    Tron_314159265359 likes this.
  48. unity_BA3036A5CF9EA7D4F796

    unity_BA3036A5CF9EA7D4F796

    Joined:
    Dec 16, 2022
    Posts:
    4
    Yep! This was the issue, and this fixed it, good job.
     
    UniqueCode and Tron_314159265359 like this.
  49. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,935
    This change pretty easily resets itself whenever you update/add a package in the package manager.

    So while it works it generally doesn't last long as Unity doesn't like it's packages being changed.
     
  50. Mj-Kkaya

    Mj-Kkaya

    Joined:
    Oct 10, 2017
    Posts:
    179
    Yes it is but, seeing the error in the Console is annoying. :rolleyes:
     
    Thygrrr likes this.