Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Bug Exception when destroying the last index in GhostPresentationGameObjectSystem.m_Entities

Discussion in 'NetCode for ECS' started by Richay, Feb 7, 2024.

  1. Richay

    Richay

    Joined:
    Aug 5, 2013
    Posts:
    120
    I'm using the GhostPresentationGameObjectPrefabReference authoring to create client-only gameobjects. However, when I destroy an entity, there's a chance of causing an IndexOutOfRangeException in GhostPresentationGameObject@147. This occurs if the entity was the final element in the m_Entities native list.

    Code (CSharp):
    1. var ghostPresentationGameObjectStateFromEntity = GetComponentLookup<GhostPresentationGameObjectState>();
    2. Entities
    3.     .WithStructuralChanges()
    4.     .WithNone<GhostPresentationGameObjectPrefabReference>()
    5.     .WithAll<GhostPresentationGameObjectState>()
    6.     .ForEach((Entity entity) =>
    7. {
    8.     var state = ghostPresentationGameObjectStateFromEntity[entity];
    9.     int idx = state.GameObjectIndex;
    10.     if (idx >= 0)
    11.     {
    12.         m_Transforms.RemoveAtSwapBack(idx);
    13.         m_Entities.RemoveAtSwapBack(idx);
    14.         var last = m_GameObjects.Count - 1;
    15.         Object.Destroy(m_GameObjects[idx]);
    16.         m_GameObjects[idx] = m_GameObjects[last];
    17.         ghostPresentationGameObjectStateFromEntity[m_Entities[idx]] = new GhostPresentationGameObjectState{GameObjectIndex = idx};
    18.         m_GameObjects.RemoveAt(last);
    19.     }
    20.     EntityManager.RemoveComponent<GhostPresentationGameObjectState>(entity);
    21. }).Run();
    In this snippet, line 17 should be skipped if idx was the last element.

    Netcode 1.2.0-pre.6.
     
    Last edited: Feb 7, 2024
  2. CMarastoni

    CMarastoni

    Unity Technologies

    Joined:
    Mar 18, 2020
    Posts:
    891
    yes, thanks for reporting!
     
  3. JesterHere

    JesterHere

    Joined:
    Dec 29, 2014
    Posts:
    10
    Would it be possible to get an ETA on this?

    It is impossible to change scene (due to ghosts being destroyed when removing the `InGame` status) without hitting this error. The ghost system doesn't recover from this.