Missile Moving to Different Target, When Instantiate

I have a missile script and launcher function in my playershoot script where i instantiate the missile and it select and random target from the list of enemy target when instantiated and then i get the missile script and assign the target which was pick randomly. but the only thing the missile randomly select one target. and instantiate two missile at that specific target, which is suppose to select two enemy and instantiate the missile for each target, but when i instantiate the missile it only go to one of the target , i want it to go to each target which is either two one all three,

the problem when i destroy a target from the list and two target remain in list , the two missile that is being instantiate only go to one of the target in the list because i use random.range. which the two missile suppose to move to separate Target, and also how can i have a curve movement when instantiate and multiple lock on with UI…
here the code

this the PlayerShoot Script

public List<Transform> missile_position = new List<Transform> ();
public GameObject missile;
public List<GameObject> enemy = new List<GameObject>();

    public void LaunchMissile ()
    {
        //Instantiate (missile, missilespawn.position, missilespawn.rotation);
        //GameObject _enemy = GameObject.FindObjectsOfType<>;
        //enemy.Add (_enemy);
        enemy = new List<GameObject>(GameObject.FindGameObjectsWithTag("Enemy"));


        if (enemy != null) {
            foreach (Transform position in missile_position)
            {
               
                if (enemy.Count > 0)
                {
                    GameObject randomenemy = enemy [Random.Range (0, enemy.Count- 1)];

                    GameObject missile2 = Instantiate (missile, position.transform.position, position.transform.rotation)as GameObject;
                    missile2.GetComponent<TestingMissile> ().Target = randomenemy;
                    print (randomenemy);
                }
               
            }
            //foreach (GameObject target in enemy) {

            //}
        }

Here the Missile Testing Script

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using PathologicalGames;

public class TestingMissile : MonoBehaviour
{

    public GameObject Target;
    private Health _Health;
    private BossHealth _BossHealth;
    public float speed;
    int _dmg = 100;
    void Start ()
    {


        //Target = GameObject.FindGameObjectsWithTag ("CurveEnemy");
    }

    void FixedUpdate()
    {

        if (Target != null) {
            LookAt (Target);
            transform.position = Vector3.MoveTowards (transform.position, Target.transform.position, speed * Time.deltaTime);
        }
        else
        {
            transform.Translate (Vector3.up * speed * Time.deltaTime);
        }
    }

    void LookAt (GameObject _target)
    {
        if (_target != null) {
            Vector3 dir = _target.transform.position - transform.position;
            float angle = Mathf.Atan2 (dir.y, dir.x) * Mathf.Rad2Deg - 90;
            transform.rotation = Quaternion.AngleAxis (angle, Vector3.forward);
        } else
            return;
        }

    void OnTriggerEnter (Collider other)
    {
        if(other.gameObject.tag == "Enemy" || other.gameObject.tag == "Boss")
        {
            //PoolManager.Pools ["Player"].Despawn (xmissile);
            Destroy (gameObject);
             _Health = other.gameObject.GetComponent<Health> ();
            if (_Health != null) {
                _Health._Dmg (_dmg);
            }

            _BossHealth = other.gameObject.GetComponent<BossHealth> ();
            if (_BossHealth != null) {
                _BossHealth.TakeDamage (_dmg);
            }


        }
    }

}

Okay, so from my understanding (I’m sorry, I’m having a terrible time trying to understand what you’re saying), you’re trying to select two random targets and fire one missile at each of them, but instead it’s choosing one random target and firing two missiles at them?

Random.Range’s second parameter is exclusive for an integer range, which means you don’t need to do “List.Count -1”, you can just do “List.Count”. Random.Range(0, 10), for instance, will return a random value between 0 and 9. This is probably one of your big problems, if you’ve only got two enemies in the scene.

1 Like

i know that i try that already the think is i want the two missile to go to separator enemy in the scene, remember the TestingMissile have public gameobject target and that is being assign by the random select of target. so if i get a random enemy from list why the other missile go to same enemy but the first missile already get the target. i want you to know that it’s two missile i instantiate and one missile must move to one of the target on the left and the other missile go to the target on the right but the two missile going to the same target that the random.range choose from the list which is left. so i want to know if i instantiate the missile and remove one target from the list so the other missile can find the next target which the random.range selected. when the target is being collided with the missile than it remove from the list and when i instantiate a next missile it going to select target from 1 or 2 cause first target have being destroy, which i want the first missile to kill two enemy from list and leave with one enemy, it work some time but it don’t.

the way i send question alway be corrupt

GameObject randomenemy = enemy [Random.Range (0, enemy.Count)];

if(enemy.Count > 1)
    enemy.Remove(randomenemy);

Try that, if I’m understanding you correctly.

so i am going to try that after school rite now i am doing some research from a presentation tomorrow