Search Unity

NullReferenceException when accessing Targets from CinemachineTargetGroup

Discussion in 'Cinemachine' started by default_team, Feb 15, 2019.

  1. default_team

    default_team

    Joined:
    Dec 1, 2018
    Posts:
    21
    I'm trying to adjust weights of targets like:

    Code (CSharp):
    1.  
    2. IEnumerator AdjustWeightOnDistance () {
    3.     yield return new WaitForSeconds(3f);
    4.  
    5.     for (int i = 1; i < targetGroup.m_Targets.Length; i++) {
    6.         float distance = Vector2.Distance(player.transform.position,
    7.         targetGroup.m_Targets[i].target.transform.position);
    8.         // ...
    9. }
    10.  

    There are other game objects that adds and removes targets to the same TargetGroup after a specific UnityEvent. And the above method is also invoked by the same UnityEvent. So I think that these run simultaneously.

    That's why I tried adding a 'WaitForSeconds' to the above method. But still I get NullReferenceException.

    Event that adds, and removes members from the TargetGroup. For some reason if I switch these two event calls, I don't get the NullReferenceException.



    Once the event is being invoked, it looks like the target is not properly added or removed.

     
    Last edited: Feb 15, 2019
  2. default_team

    default_team

    Joined:
    Dec 1, 2018
    Posts:
    21
    I think now I understand the issue little better. Say we have target1, and target2. So to add target3 and to remove target2 from group, I had to remove target2 first, only then add the target3.

    Doing otherway around causes 'None (Transform)' to appear in the targets list like in the picture..