Search Unity

Nulll Reference Problem with my missile script when all target is being Destroy

Discussion in 'Scripting' started by Tatsumi-Kun, Apr 4, 2017.

  1. Tatsumi-Kun

    Tatsumi-Kun

    Joined:
    Feb 11, 2015
    Posts:
    130
    I have a missile script where i look for a target and then chase toward it but, after the target was remove the script giving me this error

    NullReferenceException: Object reference not set to an instance of an object
    Missile.FindClosestEnemy () (at Assets/Scripts/Player_Scripts/Missile.cs:63)
    Missile.Start () (at Assets/Scripts/Player_Scripts/Missile.cs:19)

    Code (CSharp):
    1. {
    2.  
    3.  
    4.  
    5.     public string searchTag;
    6.     private GameObject closetMissile;
    7.     public Transform target;
    8.     public GameObject missileExpObject;
    9.     public int _dmg;
    10.  
    11.     void Start ()
    12.     {
    13.         closetMissile = FindClosestEnemy ();
    14.         if (closetMissile)
    15.             target = closetMissile.transform;
    16.  
    17.  
    18.  
    19.         //if (target == null) {
    20.         //    Destroy (this.gameObject);
    21.             //transform.position = Vector3.up * 10f * Time.deltaTime;
    22.  
    23.     //    } else {
    24.        
    25.        
    26.         //}
    27.     }
    28.  
    29.     void Update ()
    30.     {
    31.  
    32.  
    33.         if (target != null) {
    34.             Vector3 dir = target.transform.position - transform.position;
    35.             float angle = Mathf.Atan2 (dir.y, dir.x) * Mathf.Rad2Deg - 90;
    36.             transform.rotation = Quaternion.AngleAxis (angle, Vector3.forward);
    37.             //transform.Translate (Vector3.forward * 5.0f * Time.deltaTime);
    38.             Rigidbody rig = GetComponent<Rigidbody> ();
    39.             rig.velocity = transform.position = Vector3.MoveTowards (transform.position, target.transform.position, 10f * Time.deltaTime);
    40.    
    41.         } else {
    42.            
    43.            
    44.         }
    45.  
    46.     }
    47.  
    48.     GameObject FindClosestEnemy ()
    49.     {
    50.         GameObject gos;
    51.         gos = GameObject.FindGameObjectWithTag (searchTag);
    52.  
    53.         GameObject closest = null;
    54.         float distance = Mathf.Infinity;
    55.  
    56.         Vector3 position = transform.position;
    57.             Vector3 diff = gos.transform.position - position;
    58.             float curDistance = diff.sqrMagnitude;
    59.  
    60.             if (curDistance < distance) {
    61.                 closest = gos;
    62.                 distance = curDistance;
    63.             }
    64.  
    65.         return closest;
    66.     }
    67.  
     
  2. joshmond

    joshmond

    Joined:
    May 28, 2012
    Posts:
    34
    My guess would be that you're only setting the target on Start() which runs once, so it will find closest target and then when that target it destroyed it gives a null reference. You are also returning a null object in your "FindClosestEnemy()" method, you are returning a gameobject called "closest" but the value is always null. You may want to try something like this to make things a little simpler:



    Code (CSharp):
    1.  
    2. private bool IsInRange()
    3. {
    4.     return Vector3.Distance(target.position, missile.position) < distance;
    5. }
    6.  
    Hope this helps



    -Joshmond
     
    Last edited: Apr 4, 2017
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    If gos doesn't find anything with that tag, it will be null. Thus, most likely your error as you try to access it's transform even if it doesn't exist.
     
  4. Tatsumi-Kun

    Tatsumi-Kun

    Joined:
    Feb 11, 2015
    Posts:
    130
    when i add return null or if(object != null) FindClosestEnemy();