(UI Toolkit) "Destroy may not be called from edit mode" on game start
Asked Answered
N

3

0

The game doesn’t crash at all, I’m just getting a red error message whenever I stop the game and press the IDE’s play button again.

The error says:

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

The scene contains UIDocument with a C# script for controlling the UI.

public class MainMenuController : MonoBehaviour
{
    public Button multiplayerButton, trainingButton, accountButton, shopButton;

    void OnEnable ()
    {
        var root = GetComponent<UIDocument>().rootVisualElement;
        multiplayerButton = root.Q<Button>("multiplayer-button");
        trainingButton = root.Q<Button>("training-button");
        accountButton = root.Q<Button>("account-button");
        shopButton = root.Q<Button>("shop-button");

        multiplayerButton.clicked += MultiplayerPressed;
        trainingButton.clicked += TrainingPressed;
        accountButton.clicked += TrainingPressed;
        shopButton.clicked += ShopPressed;
    }

    void MultiplayerPressed() {
        Debug.Log("M");
    }

    void TrainingPressed() {
        Debug.Log("T");
    }

    void AccountPressed() {
        Debug.Log("A");
    }

    void ShopPressed() {
        Debug.Log("S");
    }
}
Newsome answered 23/12, 2023 at 20:21 Comment(3)

Minor yet important detail: use OnEnable for ui binding code, not Start

Lisa

Posting a full deep callstack of this exception might help identify the root cause here. It says that it was thrown at PanelSettings:OnDisable() but there is no info what called this method - this often reveals something useful about the context of the error.

Lisa

Sounds like a Unity bug. Could you please file a bug report? https://unity.com/releases/editor/qa/bug-reporting

Volatilize
F
0

Hey, I have the same problem


I tried disabling my previous prefabed EventSystem in the scene, and the error does not appear after that.


Still don’t know the root cause of the problem but hope that it will help

Fedora answered 6/6, 2023 at 2:40 Comment(0)
T
0

I still get this error, I don’t have any other EventSystem in any scene.
Normally, the whole call stack is shown on errors, for some reason it is not displayed in my editor (?)

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

Edit: Some more information: The error only appears in the editor, and does not seem to affect anything at all. I can also just follow it’s suggestion and change Destroy to DestroyImmediate, removing the error. Obviously I’d however rather not mess with immutable packages. The unity version is 2023.2.0b4.

Telencephalon answered 28/8, 2023 at 14:55 Comment(0)
P
0

I just tried updating to 2023.2.3f1, it seems to give this same error.
Anyone knows what caused it?

Peart answered 23/12, 2023 at 18:14 Comment(1)

© 2022 - 2024 — McMap. All rights reserved.