Search Unity

Resolved [SOLVED] How to reload scene?

Discussion in 'Entity Component System' started by uknowunity, Aug 7, 2020.

  1. uknowunity

    uknowunity

    Joined:
    Jul 12, 2018
    Posts:
    15
    Hey all!

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


    Code (CSharp):
    1. World.DisposeAllWorlds();
    2. DefaultWorldInitialization.Initialize("Test World", false);
    3. SceneManager.LoadScene("World", LoadSceneMode.Single);
    and im getting these errors:

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

    and

    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. 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)
    3. 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)
    4. 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)
    5. 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)
    6. 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)
    7. Unity.Entities.Editor.PlayerLoopSystemGraph.ValidateCurrentGraph () (at Library/PackageCache/com.unity.dots.editor@0.9.0-preview.1/Editor/SystemSchedule/PlayerLoop/PlayerLoopSystemGraph.cs:37)
    8. UnityEditor.EditorApplication.Internal_CallUpdateFunctions () (at <b17f35b08b864a3ca09a7032b437596e>:0)
    9.  
    Qeustion:
    What is the correct way to reload the scene without the entities that are created in runtime?
     
    Last edited: Aug 7, 2020
  2. uknowunity

    uknowunity

    Joined:
    Jul 12, 2018
    Posts:
    15
    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.


    Code (CSharp):
    1. var entitymanager = World.DefaultGameObjectInjectionWorld.EntityManager;
    2.  
    3. entitymanager.DestroyEntity(entitymanager.GetAllEntities());
    4.  
    5. SceneManager.LoadScene("World", LoadSceneMode.Single);
     
  3. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,264
    uknowunity and brunocoimbra like this.
  4. uknowunity

    uknowunity

    Joined:
    Jul 12, 2018
    Posts:
    15
    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.
     
  5. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,264
    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.
     
    uknowunity likes this.
  6. uknowunity

    uknowunity

    Joined:
    Jul 12, 2018
    Posts:
    15
    Thank you its working now!

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