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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

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..