So as in title, I couldn’t wrap my head around it and that’s the most hateful hassle in my DOTS project that is basically stopping my college internship’s progress… I’m continuously getting vague errors and crashes when I change a scene. If someone was able to help with this, at least I should virtually owe him a drink!
Code for changing a scene (in LateUpdate() of a monob):
SceneManager.LoadScene("SomeLevel");
_entityManager.DestroyEntity(_entityManager.UniversalQuery);
General architecture: gameobjects follow entities in LateUpdate(), that is necessary because my project would require some complex animations:
public class FollowEntity : MonoBehaviour
{
public Entity EntityToFollow;
...
void LateUpdate()
{
if (_entityManager.Exists(EntityToFollow))
{
Translation entityPosition = _entityManager.GetComponentData<Translation>(EntityToFollow);
transform.position = entityPosition.Value;
}
}
}
Spawner: I have one prefab for the entities and one for the gameobjects. It spawns already converted entity prefabs and their follower gameobjects altogether:
var settings = GameObjectConversionSettings.FromWorld(World.DefaultGameObjectInjectionWorld, _blobAssetStore);
Entity myPrefab = GameObjectConversionUtility.ConvertGameObjectHierarchy(myEntityPrefab, settings); // converts to entity
var myGameObj = Instantiate(myGameObjPrefab);
var myEntity = _entityManager.Instantiate(myPrefab); //instantiates an entity from an already converted entity prefab
myGameObj.GetComponent<FollowEntity>().EntityToFollow = myEntity;
When I switch to an empty scene like a menu, or when I enter play mode, all the levels works fine without errors. Yet when I change to a non-empty scene (such as Level1 → Level1 again), I get a bunch “Entity does not exist” errors (thrown from a well-functioning external navmesh system I’m using, check “dotsnav” on Google if you wish) and a Unity crash.
My hypothesis:
- the conversion I’m doing (ConvertGameObjectHierarchy in the Start() of a Spawner monob) is not so good and causes issues when respawning the scene. It’s the best method I’ve understood to get things done but I wish to understand a safer and solid conversion method;
If you have any hints about architecture, conversion or an idea of why the errors and crashes, even insulting my architecture if you feel necessary… It’d be of extreme help! As I said before, it’s a worrying issue to me.
well, thank you!
EDIT: this level-switching architecture worked fine until I added follower GameObjects for animation purpose.