Answer the question
In order to leave comments, you need to log in
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]);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question