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

Add a float to a list in a foreach statement

Discussion in 'Scripting' started by R0b_, Oct 14, 2018.

  1. R0b_

    R0b_

    Joined:
    Jul 11, 2018
    Posts:
    25
    Code (csharp):
    1.  
    2.         foreach (Transform Target in PossibleTartgets)
    3.         {
    4.             Debug.Log(Target + " | " + Vector2.Distance(transform.position, Target.position));
    5.             TargetDistance.Add(Vector2.Distance(transform.position, Target.position));
    6.         }
    7.  

    When I run this I get this error:

    NullReferenceException: Object reference not set to an instance of an object
    simpleEnemy.SetTarget () (at Assets/scripts/enemies/simpleEnemy.cs:26)
    simpleEnemy.Awake () (at Assets/scripts/enemies/simpleEnemy.cs:18)
     
  2. R0b_

    R0b_

    Joined:
    Jul 11, 2018
    Posts:
    25
    I think it could be a bug, but I'm not sure and I wouldn't know how to solve it if i was.
     
    Last edited: Oct 15, 2018
  3. AdamRamberg

    AdamRamberg

    Joined:
    Dec 8, 2016
    Posts:
    22
    Could you add the full script? Hard to debug if we don't know the correct line numbers.

    Just from looking what you have posted I guess "PossibleTartgets", any of the "Target"s, or TargetDistance could be null / uninitialized.
     
  4. R0b_

    R0b_

    Joined:
    Jul 11, 2018
    Posts:
    25
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Linq;
    5. using UnityEngine;
    6. public class simpleEnemy : MonoBehaviour
    7. {
    8.     [Header("Target")]
    9.     public List<Transform> PossibleTartgets;
    10.     private List<float> TargetDistance;
    11.     public Transform target;
    12.     public float speed;
    13.     private void Awake()
    14.     {
    15.         SetTarget();
    16.     }
    17.     void SetTarget()
    18.     {
    19.         foreach (Transform Target in PossibleTartgets)
    20.         {
    21.             Debug.Log(Target + " | " + Vector2.Distance(transform.position, Target.position));
    22.             TargetDistance.Add(Vector2.Distance(transform.position, Target.position));
    23.         }
    24.         float SumTargetDistance = TargetDistance.Sum();
    25.         float RandomTarget = Random.Range(0, SumTargetDistance);
    26.         for (int Targets = 0; Targets < PossibleTartgets.Capacity; Targets++)
    27.         {
    28.             if (TargetDistance[Targets - 1] < RandomTarget && RandomTarget > TargetDistance[Targets])
    29.             {
    30.                 target = PossibleTartgets[Targets];
    31.             }
    32.         }
    33.     }
    34.     public void FixedUpdate()
    35.     {
    36.         transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
    37.     }
    38. }
    39.  
     
  5. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,920
    From that code, AdamRamberg was correct -- you made a novice mistake by not setting up the TargetDistance list. Nothing to do with the foreach.

    PossibleTartgets is magic, since it's in the Inspector. Unity sets it up for you, filling it with tarts. But everything else in normal and works the normal programming way.
     
  6. R0b_

    R0b_

    Joined:
    Jul 11, 2018
    Posts:
    25
    Then how would I fix it (I don't really know what you mean by that)
     
  7. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Simply set it up in Awake, before you call the other method.

    Code (csharp):
    1. private void Awake()
    2. {
    3.     TargetDistance = new List<float>();
    4.     // ...
    5. }
    I'd also recommend to useTargetDistances.
     
  8. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,920
    Well, maybe. But then SetTarget would need to clear the list (they'll eventually call it more than once, right?) I feel like it was supposed to be Local. But then I'm seeing several other problems: a -1 index error in the "select target based on weighted distance", and Capacity for Count, and it doesn't appear to work anyway; FixedUpdate. I feel like this is someone over their head, possibly grabbed some things from tutorials that weren't quite right to begin with. Maybe should start with something easier.
     
  9. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Yes, you're right. Just wanted to throw in the solution to the first problem that you identified though.