public void Damage(int damage) { SoundManager.instance.explode(); int finalHealth = health - damage; for (int i = 0; i < spawnAmount; i++) { GameObject explosion = PoolManager.instance.explotionPool.GetObject(); explosion.transform.position = transform.position; explosion.SetActive(true); ExplotionController explosionController = explosion.GetComponent <ExplotionController> (); explosionController.SmallExplosion(); // If the asteroid wasn't fully destroyed, split it if (finalHealth > 0) { GameObject clone = PoolManager.instance.asteroidPool.GetObject(); if (clone != null) { AsteroidController asteroidController = clone.GetComponent <AsteroidController>(); if (asteroidController != null) { clone.transform.position = transform.position; clone.SetActive(true); if (finalHealth == 1) { asteroidController.MakeSmallAsteroid(); } else if (finalHealth == 2) { asteroidController.MakeMediumAsteroid(); } else if (finalHealth == 3) { asteroidController.MakeLargeAsteroid(); } } else { Debug.LogWarning("Asteroid pool not returning asteroids"); } } else { Debug.Log("Empty asteroid pool"); } } } GameManager.instance.OnDestroyAsteroid(finalHealth); gameObject.SetActive(false); }