Several instances destroyed when one is shot by player
I don't suspect anyone will be able to help me here, but here it goes.
I have an asteroid script that chooses a size(small, medium or large) at random when the game starts. The size variables are set in an enumerator. This created a problem when destroying a large asteroid, because the two new asteroids instantiated picked random sizes, even large, when I needed two medium asteroids. To solve this, I created another enumerator, prevAsteroid, and a preLoadedAsteroid boolean.
Everything seemed to work perfectly, until I noticed a bug. The large asteroid splits into two medium asteroids. Upon shooting the a medium asteroid, it breaks into two small asteroids, as I had intended. However, if the medium asteroid had come from a large asteroid, then its "sister asteroid" also affected. It turns right into another small asteroid, and there are three total.
I know the location of the bug, but I don't know how to fix it. Here is the location of the bug.
} else if (!sizeChosen && !preloadedAsteroid)
{
switch (prevAsteroid)
{
case PrevAsteroid.Small:
break;
case PrevAsteroid.Medium:
asteroidSize = AsteroidSize.Small; //right here. This effects both medium asteroids that were instantiated when the large asteroid was destroyed.
break;
case PrevAsteroid.Large:
asteroidSize = AsteroidSize.Medium;
break;
}
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.name == "BlasterBolt(Clone)")
{
switch (asteroidSize)
{
case AsteroidSize.Small:
prevAsteroid = PrevAsteroid.Small;
Instantiate(dustObject, transform.position, Quaternion.identity);
break;
case AsteroidSize.Medium:
prevAsteroid = PrevAsteroid.Medium;
Instantiate(asteroidObject, transform.position, Quaternion.identity);
Instantiate(asteroidObject, transform.position, Quaternion.identity);
Instantiate(dustObject, transform.position, Quaternion.identity);
break;
case AsteroidSize.Large:
prevAsteroid = PrevAsteroid.Large;
Instantiate(asteroidObject, transform.position, Quaternion.identity);
Instantiate(asteroidObject, transform.position, Quaternion.identity);
Instantiate(dustObject, transform.position, Quaternion.identity);
break;
}
Destroy(gameObject);
Destroy(other.gameObject);
}
While I wrote this script without any help, it was pushing the limits of my brain, and I don't even fully understand everything I did, so I'm not sure I have given enough information. Any ideas?