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

Missile Target by using string and if target if null keep search for new target

Discussion in 'Scripting' started by Tatsumi-Kun, Aug 15, 2016.

  1. Tatsumi-Kun

    Tatsumi-Kun

    Joined:
    Feb 11, 2015
    Posts:
    130
    when the missile target the enemy by using target = Getcomponent.findGameobjectwithtag<"Enemy">(). transform; is working good but when the enemy is die it giving me null reference, but i add target = get-component function to fixed update it seem to work good but it have this weird look rotation and it keep choosing new target steady. i look all over the internet and try code but not working. so i want to know if you can help me out with a new script.
    i want a array of enemy. and store this array in a string right which is findgameobjectwithstag. but if the is die i, and same time i shoot a another missile how can i tell the missile to look for a another target or if target is not found move upward on the y axis and get destroy by the boundary.
    just making a normal shmup nobody want to help me. seem like heat seeking missile carry me to my grave working on it for week now.please let this be the final i am tired of hurting my head.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MissileMove : MonoBehaviour
    5. {
    6.  
    7.     Transform target;
    8.     public int speed;
    9.     public int _dmg = 100;
    10.     private Health _Health;
    11.  
    12.  
    13.     void Awake()
    14.     {
    15.        
    16.             target = GameObject.FindGameObjectWithTag ("Enemy").transform;
    17.  
    18.     }
    19.  
    20.     void FixedUpdate()
    21.     {
    22.        
    23.             Vector3 dir = target.position - transform.position;
    24.             float angle = Mathf.Atan2 (dir.y, dir.x) * Mathf.Rad2Deg - 90;
    25.             transform.rotation = Quaternion.AngleAxis (angle, Vector3.forward);
    26.             transform.position = Vector3.MoveTowards (transform.position, target.position, speed * Time.deltaTime);
    27.  
    28.     }
    29.  
    30.        
    31.       //  transform.Rotate(0, 0, 1.5f);
    32.     void OnTriggerEnter (Collider other)
    33.     {
    34.         if (other.gameObject.tag == "Enemy") {
    35.  
    36.             _Health = other.gameObject.GetComponent<Health> ();
    37.             if (_Health != null) {
    38.                 _Health._Dmg (_dmg);
    39.             }
    40.             //other.gameObject.GetComponent<Health> ()._Dmg (_dmg);
    41.             Destroy (gameObject);
    42.  
    43.         }
    44.     }
    45.        
    46. }
    47.  
     
  2. traderain

    traderain

    Joined:
    Jul 7, 2014
    Posts:
    108
    Well, I would say check if the enemy is null if it is then check for another enemy if there is none then do some nice curve.
     
  3. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309
    If there's no enemy's on screen, then it can't check for an active GO with the tag enemy, you might wan't to use a <list> then use conditions to check if there's any during runtime,
    Code (CSharp):
    1. List<Enemies> enemies = new list<Enemies>();
    2.  
    3. Update()
    4. {
    5. enemies = GameObject.FindGameObjectsWithTag("Enemy");
    6.  
    7. if (enemies.count > 0)
    8. {
    9. // allow detection of the transform;
    10. }
    11. else
    12. {
    13. // don't run
    14. }
    15. }
    16.  
    17.  

    EDIT:

    Forgot to mention you need to remove enemies from the list when they are destroyed using

    Code (CSharp):
    1. enemies.Remove(GONAME);
     
  4. Tatsumi-Kun

    Tatsumi-Kun

    Joined:
    Feb 11, 2015
    Posts:
    130
    so if the enemy.count >0 ) the missile when follow the enemy.
    and else tell the missile to do a curve or move forward to the screen
    and where i put enemies.Remove(GoName). and what is go name