Sprite Spawning System with Array Help?

Basically I’ve got three arrays, each creates its own sprite with a spawn location and a prefab I’ve assigned. The prefabs are little rain sprites that float around on my 2D map. Once the sprite leaves the bounds of the camera it deletes itself.

I wanted to know if there was a way I could perhaps add a loop or some kind of concatenation system that could run through the array and create a new sprite once the old one has been deleted?

68133-357b2cfbc4518c54156e4e43a4e3ef3c.png

Spawn Script:

public Transform[] spawnLocations;
public GameObject[] SpawnPrefab;
public GameObject[] SpawnClone;

void Start()    {

    spawnIntelligence();
}

void spawnIntelligence() {

    SpawnClone[0] = Instantiate(SpawnPrefab[0], spawnLocations[0].transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
    SpawnClone[1] = Instantiate(SpawnPrefab[1], spawnLocations[1].transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
    SpawnClone[2] = Instantiate(SpawnPrefab[2], spawnLocations[2].transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;

}

Deletion Script:

void Start () {

}

void Update () {
    Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
    if (screenPosition.y > Screen.height || screenPosition.y < 0) {
        Destroy(this.gameObject);
	}

    if (screenPosition.x > Screen.width || screenPosition.x < 0) {
        Destroy(this.gameObject);

    }	
}

Thanks in advance. I’m still pretty new to Unity and have since switched from Java over to C#. Any help or insight would be appreciated by my friend and I!

You need to switch from build-in array to List. It give you ability to add and delete elements:

List <GameObject> Spawn_Prefab_List = new List<GameObject>();
Spawn_Prefab_List.Add(GO);
Spawn_Prefab_List.Remove(GO);

Can’t answer in more detail, sorry.

@FLy1nRabBit

Going by your snapshop and the sample script it looks like even though you have a SpawnPrefab array of 3 elements there is only one prefab. I suggest the following which allows you to have just the one public prefab property. It uses a List instead of an Array which is more effcient. It also has functionality to control your spawned ites by way of activating/deactivating the spawned items (which is the better way to go) or by adding and removing of spawned items. It also looks like you are destroying the game object in another script, so I made it so that this can be handled in the TempSpawner class.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class TempSpawner : MonoBehaviour
{
    public GameObject SpawnPrefab;
    
    public List<Transform> spawnLocations = new List<Transform>();
    public List<GameObject> SpawnClone = new List<GameObject>();
    
    void Start()
    {
        if (SpawnPrefab != null)
        {
            if (spawnLocations.Count > 0)
            {
                spawnIntelligence();
            }
            else
            {
                Debug.LogError("There are no Spawn Locations. You need add at least one Spawn Location in the inspector");
            }
        }
        else
        {
            Debug.LogError("Spawn Prefab is null. You need to set the Spawn Prefab property in the inspector");
        }
    }

    void spawnIntelligence()
    {
        for (int i = 0; i < spawnLocations.Count; i++)
        {
            AddItem(i);
        }
    }

    public void DisableItem(int item)
    {
        if ((item < spawnLocations.Count) && (SpawnClone[item] != null))
        {
            SpawnClone[item].SetActive(false);
        }
    }

    public void ReenableItem(int item)
    {
        if ((item < spawnLocations.Count) && (SpawnClone[item] != null))
        {
            SpawnClone[item].transform.position = spawnLocations[item].transform.position;
            SpawnClone[item].SetActive(true);
        }
    }

    public void RemoveItem(int item)
    {
        if ((item < spawnLocations.Count) && (SpawnClone[item] != null))
        {
            Destroy(SpawnClone[item]);
            SpawnClone.RemoveAt(item);
        }
    }

    public void AddItem(int item)
    {
        if (item < spawnLocations.Count)
        {
            SpawnClone.Add((GameObject)Instantiate(SpawnPrefab, spawnLocations[item].transform.position, Quaternion.Identity));
        }
    }

    void Update ()
    {
        for (int i = 0; i < spawnLocations.Count; i++)
        {
            Vector2 screenPosition = Camera.main.WorldToScreenPoint(SpawnClone*.transform.position);*

if ((screenPosition.y > Screen.height) || (screenPosition.y < 0) ||
(screenPosition.x > Screen.width) || (screenPosition.x < 0))
{
DisableItem(i);
}
}
}
}

Use Lists. You can add, delete an item into a List easily. But one thing I understood from your question is you’re creating rain sprites and deleting (destroying) it. Why not use object pool system. You create some sprites initially, re-use if needed, and disable them whenever you want to delete instead of destroying.

You can also use particles for your rain kind of scene.