Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Cinemachine Targetgroup Remove Member Issue - Unity 2019.1.0f2

Discussion in 'Cinemachine' started by Rumbleboxer, May 18, 2019.

  1. Rumbleboxer

    Rumbleboxer

    Joined:
    Dec 26, 2012
    Posts:
    27
    Hi guys, I'm having an issue with adding and removing targets to the cinemachine target group. I'm not sure why this is happening but I've made the following gifs below:

    Basically I have 2 button functions, one is to add a transform via the CinemachineTargetGroup.AddMember and remove the transform using CinemachineTargetGroup.RemoveMember

    Now this works completely fine if the removed member is the last member added to the list as shown in the 1st gif below.

    It does not work so proper however when the target transform being removed is higher in the list's index. The transform is removed, but the list length isn't updated and shows an empty index.

    cinemachine-removemember.gif

    On top of that, let's say I wanted to remove a target from the list. If a new item was added after that index, removing the earlier item ALSO removes the last newly added transform. And we are again left with the same empty index.

    cinemachine-removemember2.gif

    Is this a bug or am I just doing this wrongly?

    paging @Gregoryl maybe? (sorry if I got the wrong person)
     
  2. Rumbleboxer

    Rumbleboxer

    Joined:
    Dec 26, 2012
    Posts:
    27
    So I'm just gonna add that I've found a workaround if anyone has this issue...

    To remove a transform from the CinemachineTargetGroup without fudging up:

    1. use FindMember() to get the object's index.
    2. replace that index with the current item in the last position of the array (Array.Length -1)
    3. use RemoveMember to remove the transform at the last position.
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    3,858
    The code for RemoveMember doesn't look quite right:

    Library/PackageCache/com.unity.cinemachine@2.2.8/Runtime/Behaviours/CinemachineTargetGroup.cs:
    Code (csharp):
    1.  
    2.         /// <summary>Remove a member from the group</summary>
    3.         public void RemoveMember(Transform t)
    4.         {
    5.             int index = FindMember(t);
    6.             if (index >= 0)
    7.             {
    8.                 var oldTargets = m_Targets;
    9.                 m_Targets = new Target[m_Targets.Length - 1];
    10.                 if (index > 0)
    11.                     Array.Copy(oldTargets, m_Targets, index);
    12.                 if (index < m_Targets.Length - 1)
    13.                     Array.Copy(oldTargets, index + 1, m_Targets, index, m_Targets.Length - index - 1);
    14.             }
    15.         }
    16.  
    If index is > 0 and < m_Targets.Length - 1, two copies happen. That's a bug.

    @Adam_Myhill, why are you guys using an array here? You're allocating a new array every time we add or remove an item, and it looks like the code would've been a lot easier to write if you'd made m_Targets a List<Target>.
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    2,486
    Yes it's a bug, and it's been fixed in the latest version of CM (2.2.9), which is available in preview, and will be released very soon.
     
    Baste likes this.
  5. Rumbleboxer

    Rumbleboxer

    Joined:
    Dec 26, 2012
    Posts:
    27
    Glad to hear it. Thanks!