Search Unity

  1. Unity 2019.4 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  3. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

How can I make a wait function here?

Discussion in 'Scripting' started by xkuko, May 30, 2020.

  1. xkuko

    xkuko

    Joined:
    Aug 28, 2018
    Posts:
    8
    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<Enemy>();
    }


    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);
    }
    }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    7,068
  3. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,013
    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?
     
    xkuko likes this.
unityunity