[SOLVED] How to reload scene?

Hey all!

Im trying to reload the scene that the player is in with this code in systembase:

World.DisposeAllWorlds();
DefaultWorldInitialization.Initialize("Test World", false);
SceneManager.LoadScene("World", LoadSceneMode.Single);

and im getting these errors:

InvalidOperationException: object is not initialized or has already been destroyed
Unity.Entities.ComponentSystemBase.CheckedState () (at Library/PackageCache/com.unity.entities@0.13.0-preview.24/Unity.Entities/ComponentSystemBase.cs:333)
Unity.Entities.ComponentSystemBase.AfterUpdateVersioning () (at Library/PackageCache/com.unity.entities@0.13.0-preview.24/Unity.Entities/ComponentSystemBase.cs:614)
Unity.Entities.ComponentSystem.AfterOnUpdate () (at Library/PackageCache/com.unity.entities@0.13.0-preview.24/Unity.Entities/ComponentSystem.cs:63)
Unity.Entities.ComponentSystem.Update () (at Library/PackageCache/com.unity.entities@0.13.0-preview.24/Unity.Entities/ComponentSystem.cs:116)
Unity.Entities.ScriptBehaviourUpdateOrder+DummyDelegateWrapper.TriggerUpdate () (at Library/PackageCache/com.unity.entities@0.13.0-preview.24/Unity.Entities/ScriptBehaviourUpdateOrder.cs:322)

and

NullReferenceException: Object reference not set to an instance of an object
Unity.Entities.Editor.ComponentSystemBaseNode`2[TSystem,TNode].get_NameWithWorld () (at Library/PackageCache/com.unity.dots.editor@0.9.0-preview.1/Editor/SystemSchedule/PlayerLoop/ComponentSystemNodes.cs:15)
Unity.Entities.Editor.ComponentSystemBaseNode`2[TSystem,TNode].get_Hash () (at Library/PackageCache/com.unity.dots.editor@0.9.0-preview.1/Editor/SystemSchedule/PlayerLoop/ComponentSystemNodes.cs:30)
Unity.Entities.Editor.PlayerLoopSystemGraph.DidChange (Unity.Entities.Editor.IPlayerLoopNode lhs, Unity.Entities.Editor.IPlayerLoopNode rhs) (at Library/PackageCache/com.unity.dots.editor@0.9.0-preview.1/Editor/SystemSchedule/PlayerLoop/PlayerLoopSystemGraph.cs:70)
Unity.Entities.Editor.PlayerLoopSystemGraph.DidChange (Unity.Entities.Editor.IPlayerLoopNode lhs, Unity.Entities.Editor.IPlayerLoopNode rhs) (at Library/PackageCache/com.unity.dots.editor@0.9.0-preview.1/Editor/SystemSchedule/PlayerLoop/PlayerLoopSystemGraph.cs:75)
Unity.Entities.Editor.PlayerLoopSystemGraph.DidChange (Unity.Entities.Editor.PlayerLoopSystemGraph lhs, Unity.Entities.Editor.PlayerLoopSystemGraph rhs) (at Library/PackageCache/com.unity.dots.editor@0.9.0-preview.1/Editor/SystemSchedule/PlayerLoop/PlayerLoopSystemGraph.cs:55)
Unity.Entities.Editor.PlayerLoopSystemGraph.ValidateCurrentGraph () (at Library/PackageCache/com.unity.dots.editor@0.9.0-preview.1/Editor/SystemSchedule/PlayerLoop/PlayerLoopSystemGraph.cs:37)
UnityEditor.EditorApplication.Internal_CallUpdateFunctions () (at <b17f35b08b864a3ca09a7032b437596e>:0)

Qeustion:
What is the correct way to reload the scene without the entities that are created in runtime?

I think i solved it (not sure if correct fix?). I have removed the old code and put the code below in an monobehaviour script.

But still getting some errors for singleton not found in some scripts but I made an extra if statement to check if the entity is not null.

var entitymanager = World.DefaultGameObjectInjectionWorld.EntityManager;

entitymanager.DestroyEntity(entitymanager.GetAllEntities());

SceneManager.LoadScene("World", LoadSceneMode.Single);

Here’s how I do it. Destroying an EntityQuery is faster than destroying a list of entities. Also, no need to run the logic from a MonoBehaviour. https://github.com/Dreaming381/lsss-wip/blob/master/Packages/com.latios.latios-framework/Core/Core/Systems/DestroyEntitiesOnSceneChangeSystem.cs

2 Likes

Thanks! I have put my reload system now in systembase.

But how do I get an EntityQuery of all entities of the world? So far I know you need add types for the enitities you are trying to find with queries.

It is in the code. EntityManager.UniversalQuery.

In my case, I want to destroy all entities except for entities with a couple specific components, so I want to subtract components from the universal query. This isn’t actually that straightforward to do, so I instead tag the UniversalQuery with a dummy tag, adding that tag to all entities in existence. Then I use that tag in combination with my subtractive components to create a new query for what I actually want to destroy.

1 Like

Thank you its working now!

I marked this thread as solved. I hope its usefull for other beginners around here :slight_smile: