Search Unity

[Fix] Cinemachine 2.0 camera destroyed problem

Discussion in 'Cinemachine' started by caosdoar, Jun 13, 2017.

  1. caosdoar

    caosdoar

    Joined:
    Oct 29, 2016
    Posts:
    26
    Hi,

    I am having a problem when working with Cinemachine virtual cameras and multiple scenes in the editor.

    What I have is a "master scene" that contains several shared game objects, like the main character or the camera. This scene loads a playable area that includes all the virtual cameras for that area. When working on the editor both scenes are loaded in the hierarchy view and the "master scene" set as the active scene. This allows me to work on the playable area, hit run and test it.

    But in the process of unloading the playable area scene and loading it again by the "master scene" the CinemachineCore.mActiveCameras keeps a list of destroyed cameras. Probably the list while in edit mode. And the cameras does not work in game anymore.

    I can work around it by manually removing the playable area scene from the hierarchy before running the game and everything works just fine. But it might be good to have a clean transition between in-editor and in-game of the CinemachineCore state.

    I could not figure out completely why are destroyed cameras still listed but it looks like CinemachineVirtualCameraBase.OnDisable() calls UpdatePriorityQueueStatus and that is going through the path that adds the camera to CinemachineCore.

    Is anyone experience this or is it a known issue? Is there a dev that can help me with it? Should I report it as a bug?

    Thank you!
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    I have not seen this problem before. Normally, OnDisable() should remove the vcam from the static list. Besides, there is also an OnDestroy() that does the same thing. Is it possible that OnDisable() and OnDestroy() are somehow being bypassed in your situation? Perhaps you are not unloading the playable area in an entirely kosher manner?
     
  3. caosdoar

    caosdoar

    Joined:
    Oct 29, 2016
    Posts:
    26
    The way that the virtual cameras are unloaded is by not marking them as "DontDestroyOnLoad" and then loading the playable area scene from a script with "SceneManager.LoadScene". I know this is a corner case and probably no one is experiencing this issue. I will try with the new release of Cinemachine and if is still happening I might try to get a minimal repro case and figure out what is going wrong.

    By the way, I think some admin marked this thread as a sticky note by mistake.
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    Yes, a repro project would be helpful here. Feel free to send it to me if you like.
     
  5. caosdoar

    caosdoar

    Joined:
    Oct 29, 2016
    Posts:
    26
    Hi Gregory,

    I found out the real source of the problem, it has to be with CinemachineBrain camera blending and the destroy of virtual cameras. If the current active camera gets destroyed CinemachineBrain.mActiveCameraPreviousFrame references a destroyed instance and it breaks as soon it tries to access any data.

    I am attaching a project to reproduce the problem. It has two scenarios: The difficult one, with the master and playable area scenes I explained before; and one more simplier than that. I'll explain how to reproduce it with the later:

    The scene you have to open is Onescene/scene. Let me explain the set up:
    - One camera with a cinemachine brain. It has a custom blends, this is important because otherwise the problem does not happen.
    - Two virtual cameras. Camera 2 with higher priority.

    How to reproduce it:
    - Hit run
    - Delete "CM vcam2"
    - Done!

    The same thing was happening when switching from one playable area to another (or when it was been reloaded). The active cameras list looks it is correct. The only modifications to cinemachine on that project is a few Debug.Log to see when cameras are added or removed from the active list.

    I spent some time trying to find a way to fix this but I will have to go to deep on the cinemachine code. My thinking is that the virtual camera needs to inform that is been destroyed and not only disabled, and that has to be passed to the brain so it can invalidate the mActiveCameraPreviousFrame. This has a problem though, no blend will be applied because of that. Other option is to rethink blending to not access the ICinemachineCamera itself but the data needed for the blending only (the CameraState maybe?). I did not look into this second option too much as it changes quite a bit the system.

    Please, let me know if I can be of any help.
     

    Attached Files:

    Gregoryl likes this.
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    @caosdoar Hey thanks for that! I'll download and have a look.
     
  7. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    @caosdoar Thanks for the great detective work! The Cinemachine team salutes you. Here is the fix.
     

    Attached Files:

    Adam_Myhill likes this.
  8. caosdoar

    caosdoar

    Joined:
    Oct 29, 2016
    Posts:
    26
    Gregory, that is awesome! I was hoping for a response but I was not expecting a fix.

    Now, watching the changes I feel like a fool. I tried comparing mActiveCameraPrevioiusFrame with null and it did not work (something I must have done wrong) and I guessed it was not possible that way. I have to study the .NET basics again.

    Just as a confirmation. I tested all the scenarios I was having problems with and it works perfectly.

    Salute the whole Cinemachine team for me.

    Cheers.
    Oscar
     
  9. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
  10. caosdoar

    caosdoar

    Joined:
    Oct 29, 2016
    Posts:
    26
    @Gregoryl that is great. Thank you again for all the help!
     
    Adam_Myhill likes this.
  11. iamarugin

    iamarugin

    Joined:
    Dec 17, 2014
    Posts:
    883
    Hi I have the same problem with the Cinemachine 2.2.0 from the Package Manager Any chance to see this fix there?
     
  12. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    Yes, CM 2.2.6 is lurking in the shadows (it's in the staging repository) and the problem has been fixed there. It will be publicly available within a couple of weeks.
     
  13. BFS-Kyle

    BFS-Kyle

    Joined:
    Jun 12, 2013
    Posts:
    883
    Any updates on when this update will be available? This is a fairly big drawback to the package manager, especially if it takes weeks for any fixes / updates to be pushed through, as we are not able to simply apply the fix that you have provided but just have to wait for the update to be available through the package manager.
     
  14. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    Yes, that is a bit of an annoying thing. For the record, you can apply patches to packages by embedding them in your Assets folder. That makes them writable so you can patch them.

    That said, the latest CM is ready and queued for production release. Waiting for the release manager to OK it. Any day now.
     
  15. nathanjams

    nathanjams

    Joined:
    Jul 27, 2016
    Posts:
    304
    Hello, @Gregoryl

    I'm having this error with Cinemachine 2.2.7.

    If I have a blend shape file attached I get this error:

    Code (CSharp):
    1. MissingReferenceException: The object of type 'CinemachineVirtualCamera' has been destroyed but you are still trying to access it.
    2. Your script should either check if it is null or you should not destroy the object.
    3. UnityEngine.Object.get_name () (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.bindings.cs:198)
    4. Cinemachine.CinemachineVirtualCameraBase.get_Name () (at C:/Users/brett/AppData/Local/Unity/cache/packages/packages.unity.com/com.unity.cinemachine@2.2.7/Runtime/Core/CinemachineVirtualCameraBase.cs:140)
    5. Cinemachine.CinemachineBrain.LookupBlend (Cinemachine.ICinemachineCamera fromKey, Cinemachine.ICinemachineCamera toKey) (at C:/Users/brett/AppData/Local/Unity/cache/packages/packages.unity.com/com.unity.cinemachine@2.2.7/Runtime/Behaviours/CinemachineBrain.cs:659)
    6. Cinemachine.CinemachineBrain.UpdateFrame0 (System.Single deltaTime) (at C:/Users/brett/AppData/Local/Unity/cache/packages/packages.unity.com/com.unity.cinemachine@2.2.7/Runtime/Behaviours/CinemachineBrain.cs:514)
    7. Cinemachine.CinemachineBrain.ProcessActiveCamera (System.Single deltaTime) (at C:/Users/brett/AppData/Local/Unity/cache/packages/packages.unity.com/com.unity.cinemachine@2.2.7/Runtime/Behaviours/CinemachineBrain.cs:473)
    8. Cinemachine.CinemachineBrain.LateUpdate () (at C:/Users/brett/AppData/Local/Unity/cache/packages/packages.unity.com/com.unity.cinemachine@2.2.7/Runtime/Behaviours/CinemachineBrain.cs:258)
    9.  
    But if I disable the blend shape files the transition works

    If there a chance that the fix posted above wasn't applied? if so can I apply the fix posted above to Cinemachine 2.2.7?

    Thanks
    Nathan
     
    Last edited: Sep 25, 2018
  16. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    This is another issue, fixed in CM 2.2.8 (to be released shortly). If you're feeling brave you can grab a preview from our staging repo.
     
    nathanjams likes this.
  17. nathanjams

    nathanjams

    Joined:
    Jul 27, 2016
    Posts:
    304
    @Gregoryl

    Thanks for this and the offer. I have a workaround that will keep everything afloat for the moment. Happy to wait for the official release.

    Thanks again!

    Nathan
     
  18. nathanjams

    nathanjams

    Joined:
    Jul 27, 2016
    Posts:
    304
    Jumped the gun and installed the latest preview build from the staging repo and it fixed this issue. No issues so far.

    2.2.8 has been very good to me.
    Thanks!