So I want to have two balls collide, destroy themselves, and then have another ball spawn at their place (preferably with a specific velocity). When I try to attach the script to the ball, however, both instances of the ball are destroyed on contact, then immediately spawns two prefabs of the ball, since they both have the code. This causes both balls to spawn and destroy each other over and over. I have this script attached to the balls:
private Vector3 ballPosition;
void OnTriggerEnter2D (Collider2D other) {
if (other.gameObject.tag == "Ball") {
ballPosition = new Vector3 ((transform.position.x + other.transform.position.x) / 2, (transform.position.y + other.transform.position.y) / 2, 0.0f);
StartCoroutine ("RespawnBall");
}
}
IEnumerator RespawnBall () {
Instantiate (gameObject, ballPosition, Quaternion.identity);
Destroy (gameObject);
yield return null;
}
How do I make this code destroy both balls, then spawn only one instance of the prefab?