Home > front end >  How to destroy instantiated prefabs in order in Unity using C#?
How to destroy instantiated prefabs in order in Unity using C#?

Time:04-05

I am new to Unity as I am new to C#. I'm making a 2D endless runner, but I ran into some problems when I try to destroy the instantiated prefabs(level parts). I tried a few ways that I found online, but these methods destroyed the prefab that was instantiated latest(for example if I instantiated 10 level parts, only the 10th was destroyed). I'm looking for something that destroys the earliest instantiated prefabs. Thanks for your help! Have a good one :)

The code I have:

public class LevelGenerator : MonoBehaviour{

    private const float distance_to_spawn  = 100f;

    [SerializeField]private List<Transform> LevelPartList;

    [SerializeField]private Transform LevelPart0;

    [SerializeField]private GameObject player; 

    private Vector3 lastEndingBlock; 

    private Vector3 distance = new Vector3(5,3,0);

    private Transform lastLevelPartTransform;

    // the starting point of the spawning process
    private void Awake()
    {
        lastEndingBlock = LevelPart0.Find("ending_block").position;
    }
 
    // calling the spawning function, so it's infinite
    private void Update()
    {
        if(Vector3.Distance(player.transform.position,lastEndingBlock) < distance_to_spawn)
        {
            SpawnLevelPart();
        }
    }
    
    // choose randomly out of the prefabs(level parts)
    private void SpawnLevelPart()
    {
        Transform chosenLevelPart = LevelPartList[Random.Range(0,LevelPartList.Count)];
        lastLevelPartTransform= SpawnLevelPart(chosenLevelPart,lastEndingBlock - distance);
        lastEndingBlock = lastLevelPartTransform.Find("ending_block").position;
    }

    // the function used to spawn new level parts
    private Transform SpawnLevelPart(Transform LevelPart,Vector3 spawnPosition)
    {
        Transform levelPartTransform = Instantiate(LevelPart,spawnPosition,Quaternion.identity);
        return levelPartTransform;
    }
}

CodePudding user response:

Intead of keeping the spare private Transform lastLevelPartTransform; I would keep the, in a Queue like so:

private Queue<Transform> transformsToDestroy = new Queue<Transform>();

// choose randomly out of the prefabs(level parts)
private void SpawnLevelPart()
{
    Transform chosenLevelPart = LevelPartList[Random.Range(0,LevelPartList.Count)];
    gosToDestroy.Enqueue(SpawnLevelPart(chosenLevelPart,lastEndingBlock - distance));
    lastEndingBlock = lastLevelPartTransform.Find("ending_block").position;
}

So that wherever you need, you can Destroy(transformsToDestroy.Dequeue().gameObject)

CodePudding user response:

Hello Gergő Starosta! If you want to delete objects that you instantiating before, you can basically collect all instantiated objects under one parent object.

private Transform SpawnLevelPart(Transform LevelPart,Vector3 spawnPosition)
    {
        Transform levelPartTransform = Instantiate(LevelPart,spawnPosition,Quaternion.identity);
        levelPartTransform.parent = parentObject.transform;
        return levelPartTransform;
    }

After that, you can use this:

Destroy(parentObject.GetChild[ChildNumber])
  • Related