How should I properly store statistics of various items of same type?

[C#]

Im a begineer making a game based on Space Invaders, with some modifications.

In my game every enemy is going to shoot a bullet with different damage values and different sprites.
My problem is that I cant figure out a way on storing that information and every enemy then get it from the script, and shoot a bullet with that data.

I can make a script for every bullet, but that would be a mess and there must be a better way.

I also thought of making a manager for those bullets with something like a group of bullet types and its sprites, then when the function of shooting is called choosing those, but I dont know how to make that.

So each type of bullet could be a prefab. Each having the one bullet script. That script could have public variables such as “damage” that way each prefab can be edited in the inspector for that prefab to carry different values for the same set of variables. The sprites and such would also be included with the prefabs.

To make a prefab: create what you want in the scene. Drag the gameObject from the scene into the project folder. Delete the object from the scene.

To instantiate a bullet prefab from, say, a script called “Player” it would looks like this:

public class Player : MonoBeaviour
{
    public GameObject bulletPrefabType1;
    public GameObject bulletPrefabType2;
    .
    .
    .
    public GameObject bulletPrefabTypeN;

    private GameObject currentBulletType;

    void Update()
    {
        if (Input.GetKeyDown(Keycode.Space))
        {
            FireBullet(currentBulletType);
        }
    }

    void setCurrentBulletType()
    {
         currentBulletType = //some code to chose which bullet//;
    }
}

You would then drag the prefabs from the project folder into the proper public field created for each bullet type on the player’s inspector.

This is not a complete solution, just something to show you how one MIGHT handle this situation in Unity.

Best of luck to you, feel free to comment with further questions if they pertain to this exact problem.

If for whatever reason you decide each bullet type should actually be a seperate class then you may want them all to inherit from a BaseBullet class or have them all implement an interface IBulletType. This makes it simpler for other scripts to manipulate bullets without having to know ecaxtly which bullet it is for functions that should apply to all bullets.

I would use a public array to store bullet types:

public GameObject bullets[];

void Shoot(int type){
   //Shoot bullet 
   Instantiate(bullets[type], Transform.position,
   Transform.rotation)
}

Add the prefabs to the public array and you should be fine :slight_smile:

Also, mark correct answers as such to mark the question as answered.