Hi,
I have a game where you have a tank and right now there a 2 turrets that look at the player and instantiate a Projectile prefab in a random number of seconds. The problem is when i try to destroy the projectile after collision the turrets can no longer shoot. It also gives me the error “The object of type ‘Rigidbody’ has been destroyed but you are still trying to access it.”
My TurretShoot Script
public Rigidbody clone;
public float speed;
public int minTime;
public int maxTime;
// Use this for initialization
void Start ()
{
StartCoroutine("WaitAndShoot");
}
// Update is called once per frame
void Update ()
{
}
IEnumerator WaitAndShoot()
{
// suspend execution for 5 seconds
while (true)
{
yield return new WaitForSeconds (Random.Range(minTime, maxTime));
clone = Instantiate (clone, transform.position, transform.rotation) as Rigidbody;
clone.AddForce (transform.forward * speed);
}
}
}
My Destroy projectile script
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
void OnCollisionEnter(Collision collision) {
Destroy (gameObject);
}
}
I couldnt get either of these to work. The first one gave me the same result and the second one it wasnt showing it was correct in monodevelop. Thank you tho.