Home > other >  How can I delete an instance of my background?
How can I delete an instance of my background?

Time:10-08

Currently in my game I have a simple road and a 3D background imported from blender. At the end of the road I have a box collider that when the player touches it, it moves the road forward (endless runner), which works fine. The 3D background is also triggered to spawn by the box collider but it just creates a new instances of it self.

My question is how can I delete the old instances of the background cuz right now they are just piling up next to each other.

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

public class TileSpawner : MonoBehaviour
{
    private int initAmount = 5;
    private float plotsize = 10f;
    private float lastXPos = 15f;

    public List<GameObject> plots;
    // Start is called before the first frame update
    void Start()
    {
        for (int i = 0; i < initAmount; i  )
        {
            SpawnPlot();

        }
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void SpawnPlot() {
        GameObject plot = plots[Random.Range(0, plots.Count)];
        float xPos = lastXPos   plotsize;
        Instantiate(plot, new Vector3(xPos, 51.08614f, 25.10745f), plot.transform.rotation);
        lastXPos  = plotsize;
    }

}

CodePudding user response:

Im not exactly sure how that works for you but cant you just make a collider behind the player and make it destroy the background on collision using OnCollision()?

CodePudding user response:

At the moment, your code is instantiating new plots, but not keeping a reference to those instantiated plots.

One solution is to keep your plot references in a Queue, and when appropriate, Destroy the object at the front of the queue.

public class TileSpawner : MonoBehaviour
{
    private int initAmount = 5;
    private float plotsize = 10f;
    private float lastXPos = 15f;

    public List<GameObject> plots;

    private Queue<GameObject> _queue;

    void Start ( )
    {
        _queue = new Queue<GameObject> ( );
        for ( int i = 0; i < initAmount; i   )
            SpawnPlot ( );
    }

    public void SpawnPlot ( )
    {
        var plot = plots [ Random.Range ( 0, plots.Count ) ];
        var xPos = lastXPos   plotsize;
        // for readability
        var go = Instantiate ( plot, new Vector3 ( xPos, 51.08614f, 25.10745f ), plot.transform.rotation );
        _queue.Enqueue ( go );
        lastXPos  = plotsize;

        // You could possibly call RemovePlot here
        // RemovePlot ();
    }

    public void RemovePlot ( )
    {
        if ( _queue.Count <= 0 ) return;
        var go = _queue.Dequeue ( );
        if ( go == null ) return;
        Destroy ( go );
    }
}

When exactly you want to remove the old plots is up to you, but a logical point could be within the spawning method itself, as when you add a new plot, you would presumably want to remove an old plot. But, that's something for you to determine.

  • Related