Is there a way to help me achieve this behavior?

i have a this simple object spawner

    [Header("Pickups to Spawn")]

    [Header("WEAPONS")]

    [Header("Electricity Gun")]
    public Transform[] SpawnPoints1;
    public GameObject ElectricityGun;
    public Text CurrentAmmoText;
    public int currentAmmo;
    public int maxAmmo;
    public int pickupAmmo;

    [Header("Missle")]
    public Transform[] SpawnPoints2;
    public GameObject Missle;
    public Text CurrentMissleAmmoText;
    public int currentMissleAmmo;
    public int maxMissleAmmo;
    public int MisslepickupAmmo;

    [Header("ITEMS")]

    [Header("Health")]
    public Transform[] SpawnPoints2;
    public GameObject HealthPickup;
    public int HealthAmount = 10;


    void spawn()
    {
        for (int i = 0; i < SpawnPoints1.Length; i++) {
            Instantiate (ElectricityGun, SpawnPoints1[i].position, Quaternion.identity);
        }

    
for (int i = 0; i < SpawnPoints2.Length; i++) {
            Instantiate (Missle, SpawnPoints2[i].position, Quaternion.identity);
        }

        for (int i = 0; i < SpawnPoints2.Length; i++) {
            Instantiate (HealthPickup, SpawnPoints2[i].position, Quaternion.identity);
        }
    }


    void Awake () {
        spawn ();
    }

what this does is get a weapon pickup prefab and spawn it to its spawnpoints, but i quickly realized that if i keep going this could get real ugly and redundant script, so my question , is there a way to simplify/cleanup this?

maybe a way to click 1 button which would automatically add the variables for each item (such as current ammo, max ammo spawn to their spawnpoints etc) and i would only have to fill them up in the inspector?. also as i keep adding weapons, the inspector will get very busy with all the information about each item to be spawned etc so is there an cleaner method to do this? basically to not make a huge script of redundant stuff that maybe a method to do it more cleanly would do…

thanks!

Yes, there are better ways to do this, but best would be to not do it at all. This appears to be doing nothing but instantiating a bunch of objects in various positions. The standard Unity Way of doing that is to simply put them all under some GameObject in your scene hierarchy, and then make a prefab out of it. Then you simply instantiate that prefab, and presto! All the objects appear, in their correct positions and orientations, with whatever custom properties you want to assign to each one, etc.

2 Likes
    [Header("WEAPONS")]
    [Header("Electricity Gun")]
    public Transform[] ElectricityGunSpawnPoints;
    public GameObject ElectricityGun;
    public int CurrentElectricityGunAmmo;
    public Text CurrentElectricityAmmoText;
    [Header("Swarm Missle")]
    public Transform[] SwarmMissleSpawnPoints;
    public GameObject SwarmMissle;
    public int CurrentSwarmMissles;
    public Text CurrentSwarmMissleAmmoText;

Well I’ve moved Ammo Amounts to the script on the prefabs, so that’s good, But for the main manager script i have those same values for each weapon, Which alternative way would be good to do the same thing but instead of writing 4 lines of codes for each weapon maybe a way to make it 1 line for each weapon ?