Instantiating Multiple Projectiles *Collision Detection*

Hi,

Just BEFORE the First Instantiated projectile meets collision you instantiate another one. The second stops as if it has collided with something only because the first has met the collision.

The replied code had some errors: 'Push' and the list template declaration. It also gave me a run-time error for instantiating the Transform type Prefab. I stuck with what I originally had which was GameObject types, so I used a List that accepts that type.

public class GunScript : MonoBehaviour
{
    public GameObject BulletPreFab;
    public static GameObject instBullet; 
    //public static List<GameObject> Bullet = new List<GameObject>( );

    public void Update()
    {
       if (Input.GetKeyDown("f"))
       {
           instBullet = (GameObject) Instantiate(BulletPreFab, transform.position, Quaternion.identity);
           // Give our Bullet some initial velocity.
           instBullet.rigidbody.velocity = transform.TransformDirection(-Vector3.forward * 255);
           //Bullet.Add(instBullet); 

       }
    }

 }

public class CollisionScript : MonoBehaviour
{
    public void OnCollisionEnter (Collider other)
    {
       rigidbody.velocity = Vector3.zero;
    }
}

This seems to be the barebones. I am experiencing the same result as the question for this post describes.

There are several ways to go about this. You would not necessarily need an container of projectiles, but that is one way to go. Another way would be to use a static variable and let the projectiles stop themselves.

Loop through each instance

Make the array public and static so that it is easily accessible and then loop through the array and stop each projectile instance.

GunScript.cs (where you instantiate your projectiles)

using UnityEngine;
using System.Collections.Generic;

public class GunScript : MonoBehaviour {
    public static List<Transform> projectiles = new List<Transform>();
    public Transform prefab;

    //..somewhere..
    if(Input.GetButtonUp("Fire1")) {
        Transform shot = Instantiate(prefab, position, rotation) as Transform;
        projectiles.Add(shot);
    }
    //...
}

ColliderScript.cs (on the collider)

using UnityEngine;

public class ColliderScript : MonoBehaviour {
    public void OnCollisionEnter (Collision collision) {
        Debug.Log("You hit something");

        if (!audio.isPlaying) {
            audio.clip = CollisionSound;
            audio.Play();
        }

        foreach(Transform shot in GunScript.projectiles)
            shot.rigidbody.velocity = Vector3.zero;
    }
}

Let each instance stop itself

Store an accessible flag common to all projectiles and let them stop themselves, but will involve checking a boolean every frame.

ProjectileScript.cs (on the projectile prefab)

public class ProjectileScript : MonoBehaviour {
    public static bool allStopped = false; //This will stop them all
    bool stopped = false; //This will be indicative of each projectile

    public void Update() {
        if(allStopped && !stopped) {
            rigidbody.velocity = Vector3.zero;
            stopped = true;
        }
    }
}

ColliderScript.cs (on the collider)

public class ColliderScript : MonoBehaviour {
    public void OnCollisionEnter (Collision collision) {
        Debug.Log("You hit something");

        if (!audio.isPlaying) {
            audio.clip = CollisionSound;
            audio.Play();
        }

        ProjectileScript.allStopped = true;
    }
}