Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Scene delay

Discussion in 'Scripting' started by Zuigi114, Sep 12, 2022.

  1. Zuigi114

    Zuigi114

    Joined:
    Aug 22, 2022
    Posts:
    3
    Hello, I am relatively new to Unity, I was making a 2D project, and within that was making a Main Menu, it seems, that everytime I click play, it gives it a 5 second delay which is bizzare, and i dont know what causes this delay? does anyone know what the problem is, here is the code:



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

    public class LevelManager : MonoBehaviour
    {
    [SerializeField] float LoadDelay = 0f;
    public void LoadGame()
    {
    ReloadScene();
    SceneManager.LoadScene("Game");
    }

    public void QuitGame()
    {
    Debug.Log("Quitting");
    }

    IEnumerator ReloadScene()
    {
    yield return new WaitForSecondsRealtime(LoadDelay);
    FindObjectOfType<ScenePersist>().ResetScenePersist();
    }

    }
     
    Last edited: Sep 12, 2022
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,148
    I'm not sure what your FindObjectOfType is trying to do, but keep in mind, nothing is happening right now like you think.

    Your coroutine isn't being called correctly and even if it was, it would get called, hit the yield, and then return to LoadGame to call the next line, which loads the game scene. The coroutine does not add a pause into the LoadGame call.
     
  3. Zuigi114

    Zuigi114

    Joined:
    Aug 22, 2022
    Posts:
    3
    The FindObjectOfType, is getting a function from a script called "ScenePersists" In which destroys the scenes current avatars, before loading a new scene
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,148
    Well, hate to break it to you, but that's not what your code will do. If you want that call to happen before the scene load, you need the scene load call after it. Then, make sure you look up how to start a coroutine correctly so that it runs.

    But also, looking at this, do you need a coroutine for this part? Are you thinking you need a delay?
     
    Bunny83 likes this.
  5. Zuigi114

    Zuigi114

    Joined:
    Aug 22, 2022
    Posts:
    3
    I do not know, I think it may be on Unity's side, because the code seems to be working on another pc
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,148
    Changing the scene will destroy stuff in the previous scene if you aren't doing additive load. The coroutine itselff is not running with the code you posted.
     
    Bunny83 likes this.
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    Be careful above: changing that number will have no effect on scenes / prefabs. Here is why:

    Serialized properties in Unity are initialized as a cascade of possible values, each subsequent value (if present) overwriting the previous value:

    - what the class constructor makes (either default(T) or else field initializers)

    - what is saved with the prefab

    - what is saved with the prefab override(s)/variant(s)

    - what is saved in the scene and not applied to the prefab

    - what is changed in Awake(), Start(), or even later etc.

    Make sure you only initialize things at ONE of the above levels, or if necessary, at levels that you specifically understand in your use case. Otherwise errors will seem very mysterious.

    Field initializers versus using Reset() function and Unity serialization:

    https://forum.unity.com/threads/sensitivity-in-my-mouselook-script.1061612/#post-6858908

    https://forum.unity.com/threads/crouch-speed-is-faster-than-movement-speed.1132054/#post-7274596

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    You may edit your post above.
     
    Bunny83 likes this.