I am creating a game, with-in the game there's 7 GameObject Prefabs, each one Instantiate it's on level, I am now creating the main script that will instantiate each GameObject Prefab, now what I want to do, is so create each 80f "seconds" to instantiate a new GameObject Prefab, and destroy the last one, for example, in 0f seconds since Level Started, Instantiate (Random) GameObject Prefab, and in 80f seconds destroy the one I created in 0f seconds, and Instantiate a new one, and so on.
This is the script that I have right now, it doesn't work, it does Instantiate new gameobject, but do not destroy the last one I created. I hope you can help me / give me an idea how to solve this problem NOTE I tried using Stack, but when I destroy Prefab from the stack, it destroyes the prefab it self, and it's not recoverable.my script:
TimeSinceLevelStarted = Time.timeSinceLevelLoad;
if (Mathf.Clamp(TimeSinceLevelStarted - TimeToLoadNext, -2f, 2f) == TimeSinceLevelStarted - TimeToLoadNext
&& LoadedFirstLevel == false)
{
GameObject go = LevelsPrefabs[Random.Range(0, LevelsPrefabs.Length)];
FirstLevel(go);
LoadedFirstLevel = false;
Debug.Log("Instantiated Prefab2");
}
}
private void FirstLevel(GameObject go)
{
if (LoadedFirstLevel == false)
{
Instantiate(go, new Vector3(0, 0, 0), Quaternion.identity);
goStack.Push(go);
Debug.Log("Instantiated Prefab1");
TimeToLoadNext += 50f;
LoadedFirstLevel = false;
if (TimeToLoadNext >= 30f) {
Destroy(go);
}
}
}
Destroy
has an overload taking a delay in seconds likeDestroy(obj, 80.0f);
– derHugo