I got to thinking about adding asteroids and destroyable objects and didn’t see a need in re-inventing the wheel so I added a enemyShoots Boolean to be accessible in the unity editor to allow toggling of the fire system for the enemies off and on. This then lets me set it off and I don’t have asteroids that shoot bullets or asteroids that generate object errors by excluding a prefab laser.
in the initialization I added:
public class Enemy : MonoBehaviour
{
[Header(“Enemy Primary Settings”)]
[SerializeField] float health = 1;
[SerializeField] int pointsValue = 100;
[SerializeField] bool enemyShoots = true;
}
then all you have to do is go ahead and assign a laser prefab to it in the editor and it won’t spawn object missing errors. However, this will keep it from firing a weapon if set to false.
private void Update()
{
if (enemyShoots)
{
CountDownAndShoot();
}
}
Short, sweet and simple and works without having to make special code just for asteroids or other obstacles that don’t shoot but present collision hazards for the player. In theory you could have say space mines that could operate the same way if you wanted.