(UI Toolkit) "Destroy may not be called from edit mode" on game start
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 Start()
{
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");
}
}
Minor yet important detail: use OnEnable
for ui binding code, not Start
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.
Answer by OleksiyNosov · Jun 03 at 03:01 PM
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
Your answer
