Search Unity

Null problem

Discussion in 'Scripting' started by forkmor, Sep 30, 2014.

  1. forkmor

    forkmor

    Joined:
    Sep 30, 2014
    Posts:
    6
    very simple script but I am new to this. could someone just tell me what im doing wrong. im getting this in the console and of course not getting any results
    Field `Nav.bestTarget' is never assigned to, and will always have its default value `null'


    Code (CSharp):
    1. public class Nav : MonoBehaviour
    2. {
    3.  
    4.  
    5.     NavMeshAgent agent;
    6.     Transform bestTarget;
    7.     public GameObject[] trees;
    8.     void Start ()
    9.     {
    10.         agent = GetComponent<NavMeshAgent> ();
    11.  
    12.     }
    13.    
    14.     // Update is called once per frame
    15.     void Update ()
    16.     {
    17.  
    18.         trees = GameObject.FindGameObjectsWithTag ("tree");
    19.  
    20.  
    21.  
    22.         agent.SetDestination (bestTarget.position);
    23.     }
    24.    
    25.     Transform GetClosestTree ( GameObject[] trees)
    26.     {
    27.         Transform bestTarget = null;
    28.         float closestDistanceSqr = Mathf.Infinity;
    29.         Vector3 currentPosition = transform.position;
    30.         foreach(GameObject potentialTarget in trees)
    31.         {
    32.             Vector3 directionToTarget = potentialTarget.transform.position - currentPosition;
    33.             float dSqrToTarget = directionToTarget.sqrMagnitude;
    34.             if(dSqrToTarget < closestDistanceSqr)
    35.             {
    36.                 closestDistanceSqr = dSqrToTarget;
    37.                 bestTarget = potentialTarget.transform;
    38.             }
    39.         }
    40.        
    41.         return bestTarget;
    42.     }
    43. }
    44.  
     
  2. forkmor

    forkmor

    Joined:
    Sep 30, 2014
    Posts:
    6
    im trying to get the character to move to the closest tree out of the trees array, and ive scrapped this script together and just needed some assistance
     
  3. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    you never call GetClosetTree
     
  4. forkmor

    forkmor

    Joined:
    Sep 30, 2014
    Posts:
    6
    example?
     
  5. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    Haven't checked it but try this

    Code (CSharp):
    1.     public class Nav : MonoBehaviour
    2.     {
    3.    
    4.    
    5.         NavMeshAgent agent;
    6.         Transform bestTarget;
    7.         public GameObject[] trees;
    8.         void Start ()
    9.         {
    10.             agent = GetComponent<NavMeshAgent> ();
    11.    
    12.         }
    13.      
    14.         // Update is called once per frame
    15.         void Update ()
    16.         {
    17.    
    18.             trees = GameObject.FindGameObjectsWithTag ("tree");
    19.    
    20.      bestTarget=GetClosestTree (trees)
    21.    
    22.             agent.SetDestination (bestTarget.position);
    23.         }
    24.      
    25.         Transform GetClosestTree ( GameObject[] trees)
    26.         {
    27.             Transform result = null;
    28.             float closestDistanceSqr = Mathf.Infinity;
    29.             Vector3 currentPosition = transform.position;
    30.             foreach(GameObject potentialTarget in trees)
    31.             {
    32.                 Vector3 directionToTarget = potentialTarget.transform.position - currentPosition;
    33.                 float dSqrToTarget = directionToTarget.sqrMagnitude;
    34.                 if(dSqrToTarget < closestDistanceSqr)
    35.                 {
    36.                     closestDistanceSqr = dSqrToTarget;
    37.                     result = potentialTarget.transform;
    38.                 }
    39.             }
    40.          
    41.             return result;
    42.         }
    43.     }
    44.    
    45.  
     
  6. forkmor

    forkmor

    Joined:
    Sep 30, 2014
    Posts:
    6
    Except for one semicolon lol it worked like a charm!
    Thanks a lot!