Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Code works in the editor but breaks in build

Discussion in 'Entity Component System' started by Bezoro, Jun 23, 2023.

  1. Bezoro

    Bezoro

    Joined:
    Mar 16, 2015
    Posts:
    129
    Code (CSharp):
    1. public class TestAuthoring : MonoBehaviour
    2. {
    3.     public CinemachineVirtualCamera CinemachineVirtualCamera;
    4.  
    5.     private class Baker : Baker<TestAuthoring>
    6.     {
    7.         public override void Bake(TestAuthoring authoring)
    8.         {
    9.             var entity = GetEntity(authoring, TransformUsageFlags.Dynamic);
    10.  
    11.             AddComponentObject(entity,
    12.                                new TestCinemachineComponent()
    13.                                {
    14.                                    CinemachineVirtualCamera =
    15.                                               authoring.CinemachineVirtualCamera
    16.                                });
    17.         }
    18.     }
    19. }
    20.  
    21. public class TestCinemachineComponent : IComponentData
    22. {
    23.     public CinemachineVirtualCamera CinemachineVirtualCamera;
    24. }
    This code doesn't throw any errors in the editor, but fails in builds.

    _Default-URP-2022LTS_d8iRWoSRUY.png
     
  2. DreamersINC

    DreamersINC

    Joined:
    Mar 4, 2015
    Posts:
    130
    Not a bug. Baking happens at build. Cinemachine camera doesn't exist at build. You will need to move this out of the subscene and manually create the data entity. Additional gameobjects are destroyed at bake.
     
  3. Bezoro

    Bezoro

    Joined:
    Mar 16, 2015
    Posts:
    129
    I was under the impression that baking happened in editor as well.
    That is a pretty annoying stumbling point.

    I managed to work around it by changing the type to GameObject and whenever I need a reference to the actual CinemachineVirtualCamera component I just do a regular GetComponent<T>() from the System.
    Annoying though.