How to restart a ECS scene with physics

I’m trying to implement a Restart button for a user in a ECS scene with physics (Havok).

The world is mostly setup in a scene from the start. So I want to reset everything in the scene and run it again. Here’s what I came up with:

var entityManager = Unity.Entities.World.DefaultGameObjectInjectionWorld.EntityManager;

entityManager.DestroyEntity(entityManager.UniversalQuery);
Unity.Entities.World.DisposeAllWorlds();
DefaultWorldInitialization.Initialize("Default World", false);

SceneManager.LoadScene(SceneManager.GetActiveScene().name, LoadSceneMode.Single);

I launch it from a coroutine as some people suggested in the forums.

It kinda works but the simulation is accelerated after the restart in the editor for a second and runs at about 5 FPS on an Android device (60 at start).

What am I doing wrong and how to properly implement a restart-scene button in ECS?

You don’t really need to dispose all worlds and initialize again, just destroying all entities should give the desired results

1 Like

Thanks brunocoimbra. Removing the world disposal seems to be doing the trick.

var entityManager = Unity.Entities.World.DefaultGameObjectInjectionWorld.EntityManager;
entityManager.DestroyEntity(entityManager.UniversalQuery);
SceneManager.LoadScene(SceneManager.GetActiveScene().name, LoadSceneMode.Single);
5 Likes