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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

[Cinemachine] Array index is out of range?

Discussion in 'Cinemachine' started by Glowing_Slab, Aug 31, 2018.

  1. Glowing_Slab

    Glowing_Slab

    Joined:
    Jun 19, 2015
    Posts:
    43
    I occasionally pick up lots of errors from the Cinemachine FreeLook cameras I'm using. Am I missing something obvious?

    Error is:

    IndexOutOfRangeException: Array index is out of range.
    Cinemachine.CinemachineFreeLook.UpdateCachedSpline () (at /Users/me/Library/Unity/cache/packages/packages.unity.com/com.unity.cinemachine@2.2.7/Runtime/Behaviours/CinemachineFreeLook.cs:634)
    Cinemachine.CinemachineFreeLook.GetLocalPositionForCameraFromInput (Single t)

    ... which makes me wonder if it's something to do with the vcam initial position?
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,208
    I've never seen those. If you can manage to repro consistently, please file a bug report with full repro steps and we'll get right on it.
     
  3. SideEffectsDave

    SideEffectsDave

    Joined:
    Jul 16, 2018
    Posts:
    1
    We get the same error (seemingly at random and only within the editor). We can reproduce it (more or less consistently) when we build the project or publish/update the scene containing the Cinemachine Brain using Unity Collaborate. It first appeared when someone copied a Freelook Camera in the scene containing the Cinemachine Brain and published the changes using Unity Collaborate. After that the Freelook Cameras started exposing their Rigs in the hierarchy when activated/deactivated.

    To fix it we open a scene not containing a cinemachine brain then reimport the scene containing the Cinemachine Brain and the error is gone (at least for a while :D).
     
    Last edited: Sep 4, 2018
  4. Glowing_Slab

    Glowing_Slab

    Joined:
    Jun 19, 2015
    Posts:
    43
    Interesting! I also use Collaborate, and would certainly have copied a Freelook camera too. I use three Freelook cameras and two brains in the scene. I found the issue difficult to reproduce, but it only occurs in the editor. I have also seen some of the Freelook cameras exposing at least part of their rigs on occasion.
     
  5. JonneSir

    JonneSir

    Joined:
    Jun 27, 2018
    Posts:
    4
    I had this same problem. I restarted the scene and it seems to have dissapeared.
     
  6. Glowing_Slab

    Glowing_Slab

    Joined:
    Jun 19, 2015
    Posts:
    43
    Sometimes it takes a while to reappear, restarting has not solved the issue for me.
     
  7. dval

    dval

    Joined:
    Jul 25, 2012
    Posts:
    24
    Edit:
    I just removed ExecuteInEditMode from the directives in CinemachineBrain.cs.
    Works fine in playmode. No more errors in edit mode.

    Any reason to keep ExecuteInEditMode?


    Yeah, this is still a thing. I get:
    IndexOutOfRangeException: Index was outside the bounds of the array.
    Cinemachine.CinemachineFreeLook.UpdateCachedSpline () (at Library/PackageCache/com.unity.cinemachine@2.2.0/Runtime/Behaviours/CinemachineFreeLook.cs:617)
    Cinemachine.CinemachineFreeLook.GetLocalPositionForCameraFromInput (System.Single t) (at Library/PackageCache/com.unity.cinemachine@2.2.0/Runtime/Behaviours/CinemachineFreeLook.cs:597)
    Cinemachine.CinemachineFreeLook.PushSettingsToRigs () (at Library/PackageCache/com.unity.cinemachine@2.2.0/Runtime/Behaviours/CinemachineFreeLook.cs:534)
    Cinemachine.CinemachineFreeLook.InternalUpdateCameraState (UnityEngine.Vector3 worldUp, System.Single deltaTime) (at Library/PackageCache/com.unity.cinemachine@2.2.0/Runtime/Behaviours/CinemachineFreeLook.cs:283)
    Cinemachine.CinemachineCore.UpdateVirtualCamera (Cinemachine.CinemachineVirtualCameraBase vcam, UnityEngine.Vector3 worldUp, System.Single deltaTime) (at Library/PackageCache/com.unity.cinemachine@2.2.0/Runtime/Core/CinemachineCore.cs:288)
    Cinemachine.CinemachineCore.UpdateAllActiveVirtualCameras (System.Int32 layerMask, UnityEngine.Vector3 worldUp, System.Single deltaTime) (at Library/PackageCache/com.unity.cinemachine@2.2.0/Runtime/Core/CinemachineCore.cs:209)
    Cinemachine.CinemachineBrain.UpdateVirtualCameras (Cinemachine.CinemachineCore+UpdateFilter updateFilter, System.Single deltaTime) (at Library/PackageCache/com.unity.cinemachine@2.2.0/Runtime/Behaviours/CinemachineBrain.cs:293)
    Cinemachine.CinemachineBrain.LateUpdate () (at Library/PackageCache/com.unity.cinemachine@2.2.0/Runtime/Behaviours/CinemachineBrain.cs:242)


    Every second my mouse is over the Scene window. It happens more often if I move the mouse.
    So, just opening the project, I am greeted with about 40 error messages. Nothing seems to be broken gameplay wise, but it makes it hard to see actual issues.
     
    Last edited: Feb 10, 2019
  8. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,208
    I would strongly recommend upgrading to the latest version of CM, which currently is 2.2.8.
     
  9. krishnarocks1900

    krishnarocks1900

    Joined:
    Jul 6, 2019
    Posts:
    1
    restarting scene helped me and yea update to latest version first
     
  10. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    I'm getting the same error on version 2.2.8, upgraded to 2.2.9 still getting the error. Things like entering play or assembly reload seems to trigger it or make it go away.

    Never happened until yesterday after I started changing the camera's top/middle/bottom rig. Not sure what could be wrong, everything looks fine and works as it always did in play mode.

    From my initial troubleshooting the problem comes from m_CachedOrbits.Length being 0 while you are trying to use it in this for loop:

    Code (CSharp):
    1. for (int i = 0; i < 3 && cacheIsValid; ++i)
    2.                 cacheIsValid = (m_CachedOrbits[i].m_Height == m_Orbits[i].m_Height
    3.                     && m_CachedOrbits[i].m_Radius == m_Orbits[i].m_Radius);

    Changing the line:
    Code (CSharp):
    1. bool cacheIsValid = (m_CachedOrbits != null && m_CachedTension == m_SplineCurvature);
    for
    Code (CSharp):
    1. bool cacheIsValid = (m_CachedOrbits != null && m_CachedOrbits.Length > 0 && m_CachedTension == m_SplineCurvature);
    Fixes it.

    Here is a full stack of the original error:

    [Exception] IndexOutOfRangeException: Array index is out of range.
    CinemachineFreeLook.UpdateCachedSpline() Library/PackageCache/com.unity.cinemachine@2.2.9/Runtime/Behaviours/CinemachineFreeLook.cs:717
    715: bool cacheIsValid = (m_CachedOrbits != null && m_CachedTension == m_SplineCurvature);
    716: for (int i = 0; i < 3 && cacheIsValid; ++i)
    -->717: cacheIsValid = (m_CachedOrbits.m_Height == m_Orbits.m_Height
    718: && m_CachedOrbits.m_Radius == m_Orbits.m_Radius);
    719: if (!cacheIsValid)

    CinemachineFreeLook.GetLocalPositionForCameraFromInput() Library/PackageCache/com.unity.cinemachine@2.2.9/Runtime/Behaviours/CinemachineFreeLook.cs:697
    695: if (mOrbitals == null)
    696: return Vector3.zero;
    -->697: UpdateCachedSpline();
    698: int n = 1;
    699: if (t > 0.5f)

    CinemachineFreeLook.PushSettingsToRigs() Library/PackageCache/com.unity.cinemachine@2.2.9/Runtime/Behaviours/CinemachineFreeLook.cs:634
    632: CinemachineVirtualCamera.SetFlagsForHiddenChild(m_Rigs.gameObject);
    -->634: mOrbitals.m_FollowOffset = GetLocalPositionForCameraFromInput(GetYAxisValue());
    635: mOrbitals.m_BindingMode = m_BindingMode;
    636: mOrbitals.m_Heading = m_Heading;

    CinemachineFreeLook.InternalUpdateCameraState() Library/PackageCache/com.unity.cinemachine@2.2.9/Runtime/Behaviours/CinemachineFreeLook.cs:299
    297: m_YAxisRecentering.CancelRecentering();
    298: }
    -->299: PushSettingsToRigs();
    300: if (m_BindingMode == CinemachineTransposer.BindingMode.SimpleFollowWithWorldUp)
    301: m_XAxis.Value = 0;

    CinemachineCore.UpdateVirtualCamera() Library/PackageCache/com.unity.cinemachine@2.2.9/Runtime/Core/CinemachineCore.cs:299
    298: //Debug.Log((vcam.ParentCamera == null ? "" : vcam.ParentCamera.Name + ".") + vcam.Name + ": frame " + Time.frameCount + "/" + status.lastUpdateFixedFrame + ", " + CurrentUpdateFilter + ", deltaTime = " + deltaTime);
    -->299: vcam.InternalUpdateCameraState(worldUp, deltaTime);
    300: status.lastUpdateFrame = Time.frameCount;
    301: status.lastUpdateFixedFrame = FixedFrameCount;

    CinemachineCore.UpdateAllActiveVirtualCameras() Library/PackageCache/com.unity.cinemachine@2.2.9/Runtime/Core/CinemachineCore.cs:234
    232: // Do the round-robin update
    233: CurrentUpdateFilter &= ~UpdateFilter.Smart; // force it
    -->234: UpdateVirtualCamera(vcam, worldUp, deltaTime);
    235: CurrentUpdateFilter = filter;
    236: currentRoundRobin = vcam;

    CinemachineBrain.UpdateVirtualCameras() Library/PackageCache/com.unity.cinemachine@2.2.9/Runtime/Behaviours/CinemachineBrain.cs:318
    316: CinemachineCore.Instance.CurrentUpdateFilter = updateFilter;
    317: Camera camera = OutputCamera;
    -->318: CinemachineCore.Instance.UpdateAllActiveVirtualCameras(
    319: camera == null ? -1 : camera.cullingMask, DefaultWorldUp, deltaTime);

    CinemachineBrain.LateUpdate() Library/PackageCache/com.unity.cinemachine@2.2.9/Runtime/Behaviours/CinemachineBrain.cs:272
    270: filter = CinemachineCore.UpdateFilter.SmartLate;
    271: }
    -->272: UpdateVirtualCameras(filter, deltaTime);
    273: }
    274: // Choose the active vcam and apply it to the Unity camera
     
    Last edited: Nov 1, 2019
  11. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,208
    Hey @tcz8 thanks for digging into this.

    However, I'm a little puzzled that the fix you posted would make any difference. Looking at the code, it seems to me impossible that m_CachedOrbits could ever be anything other than null or an array of length 3, so the length check simply seems irrelevant. On a domain reload it would be null, and the only time it is ever assigned to is m_CachedOrbit = new Orbit[3]. Is it possible that the mod only seemed to fix the problem?

    I'd love to explore this a little further. Do you have a project that reliably reproduces the error?
     
  12. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    I get what you're saying buy the error is gone since I changed the line.

    If you want you can clone my "ALPHA" project in my Collaborate and change the line back.
     
    Last edited: Nov 19, 2019