How can I make a wait function here?

using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine;
using System.Threading;

public class LevelController : MonoBehaviour
{
private static int _nextLevelIndex = 1;
private Enemy[ ] _enemies;

private void OnEnable()
{
_enemies = FindObjectsOfType();
}

void Update()
{
foreach(Enemy enemy in _enemies)
{
if (enemy != null)
return;
}

Debug.Log(“Zabiles wszystkich wrogow”);

_nextLevelIndex++;
string nextLevelName = “Level” + _nextLevelIndex;
string NextLevel = “NextLevel”;
SceneManager.LoadScene(NextLevel);
//<–Wait here for 3 sec
SceneManager.LoadScene(nextLevelName);
}
}

How to report problems productively in the Unity3D forums:

http://plbm.com/?p=220

Help us help you.

Please use code tags .

You really don’t want to insert waits into your Update function, it will stall your entire game while you wait. (Unity is waiting for you to finish your Update before it continues with other stuff, so if you wait the whole game stops.)

You could either move that code into a coroutine (which can wait), or restructure your code so to eliminate the wait (e.g. by putting that code in an “if” statement that somehow depends on the game clock and how much time has passed).

But, uh, waiting 3 seconds between loading one scene and loading another scene is a pretty weird thing to do, are you 100% sure that’s even what you want?

1 Like