G
G
Gregory2562022-04-11 18:08:38
Unity
Gregory256, 2022-04-11 18:08:38

How to implement object re-deletion?

public Transform currentPoint;
    public Transform[] points;
    public TextMeshProUGUI levelText;
    public float speed;
    public int index = 1;
    public float levelPosY;
    public static bool isLevelEnd = false;
    public static bool isLevelReloaded = false;
    private readonly List<GameObject> levels = new List<GameObject>();
    public static LevelManager Instance { get; private set; }

private void Start()
    {
        StartCoroutine(InstantiateLevel(index, 0));
    }
    public void Dublicate()
    {
        if (!Instance)
        {
            Instance = this;
        }
        else
        {
            DestroyImmediate(gameObject);
            Instance = this;
        }
    }
    private void FixedUpdate()
    {
        if (isLevelEnd)
        {
            index++;
            levelPosY += 30f;
            StartCoroutine(InstantiateLevel(index, 1f));
            Invoke("DestroyLevel", 3);
        }
        if (isLevelReloaded)
        {
            ReloadLevel();
            StartCoroutine(InstantiateLevel(index, 1f));
            isLevelReloaded = false;
        }
    }
    public IEnumerator InstantiateLevel(int index, float delay)
    {
        yield return new WaitForSeconds(delay);
        Vector3 position = new Vector3(0f, 0f, 0f);
        Addressables.InstantiateAsync(($"Level {index}"), position, Quaternion.identity, null, true).Completed +=
            delegate (AsyncOperationHandle<GameObject> handle)
            {
                levels.Add(handle.Result);
            };
    }
    public void DestroyLevel()
    {
        Addressables.ReleaseInstance(levels[index - 1]);
    }
    public void ReloadLevel()
    {
        Destroy(levels[index]);
    }

When the isLevelReloaded block is executed on the first load, everything works, however, on repeated loads, the previous levels are not deleted and overlap each other, what could be the problem?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question