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. Dismiss Notice

Delay next scene with scenemanager script

Discussion in 'Scripting' started by Loykymar, Jan 21, 2022.

  1. Loykymar

    Loykymar

    Joined:
    May 23, 2021
    Posts:
    7
    Hi, I got a wee issue with a script of mine as two things wont work hand in hand, here I'm trying to use the Bold code to put into the second one , im really new to coding so please be kind LOL

    the following works fine but has no delay
    ------------------------------------------------------------------------------
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6.  
    7. public class ChangePage : MonoBehaviour
    8. {
    9.    public void SceneLoader(int SceneIndex)
    10.     {
    11.         SceneManager.LoadScene(SceneIndex);
    12.     }
    -------------------------------------------------------------------------------

    SceneIndex will not work somehow (I can choose which scene but only in script and that's not what I want)
    ------------------------------------------------------------------------------
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class LoadLevelAfterTime : MonoBehaviour
    7. {
    8.     public void ButtonFunction()
    9.     {
    10.         StartCoroutine(DelaySceneLoad());
    11.     }
    12.  
    13.     IEnumerator DelaySceneLoad()
    14.     {
    15.         yield return new WaitForSeconds(1f); // Wait 1 seconds
    16.         SceneManager.LoadScene("Scene1");
    17.     }
    -------------------------------------------------------------------------------
    As i have a lot of different scenes it would be much more work than its needed

    My plan is to change to a scene (which scene i choose in the Inspector) with a selected time delay (1-2sec)

    Hope its clear what I want here LOL

    Thanks for your help
     
    Last edited: Jan 21, 2022
  2. Putcho

    Putcho

    Joined:
    Jun 1, 2013
    Posts:
    246
    make sure you have your scenes added to the BuildSetting

     
  3. Loykymar

    Loykymar

    Joined:
    May 23, 2021
    Posts:
    7
    Hi, Yeah build is always updated when a new scene is created.

    The top script works perfect for switching scenes but i like to have a wee delay but the bottom script wont let me place the top script into the bottom one the same way it comes up with an error. (The name 'SceneIndex' does not exist in the current context)

    Ill tried the following and the scene switches straight over and not after time and that is my issue - i like to have the following script just with time delay which is not working right now, unfortunately :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class LoadLevelAfterTime : MonoBehaviour
    7. {
    8.     public void ButtonFunction()
    9.     {
    10.         StartCoroutine(DelaySceneLoad());
    11.     }
    12.  
    13.     IEnumerator DelaySceneLoad()
    14.     {
    15.         yield return new WaitForSeconds(1f); // Wait 1 seconds
    16.     }
    17.  
    18.     public void SceneLoader(int SceneIndex)
    19.     {
    20.         SceneManager.LoadScene(SceneIndex);
    21.     }
    22. }
    any other way i tried it comes up with an error - i could place the SceneManager under the yield return but than i would need a script for every single scene switch which is not optimal
     
    Last edited: Jan 21, 2022
  4. Putcho

    Putcho

    Joined:
    Jun 1, 2013
    Posts:
    246
    you dont need to try oder way around yet, what you need is C# for beginner starting from the basic

    Code (CSharp):
    1. public class LoadLevelAfterTime : MonoBehaviour
    2. {
    3.     public void ButtonFunction(int SceneIndex)
    4.     {
    5.         StartCoroutine(DelaySceneLoad(SceneIndex));
    6.     }
    7.  
    8.     IEnumerator DelaySceneLoad(int SceneIndex)
    9.     {
    10.         yield return new WaitForSeconds(3f); // Wait 1 seconds
    11.  
    12.         SceneManager.LoadScene(SceneIndex);
    13.     }
    14.  
    15.     public void SceneLoader(int SceneIndex)
    16.     {
    17.         SceneManager.LoadScene(SceneIndex);
    18.     }
    19. }
    you are try to use IEnumerator DelaySceneLoad without implement a parameter for it and that why you got (The name 'SceneIndex' does not exist in the current context) error

     
  5. Loykymar

    Loykymar

    Joined:
    May 23, 2021
    Posts:
    7
    Cheers mate that's exactly what I was looking for, couldn't get my head around what I was doing wrong.

    Cheers for the link, watched a couple vids from them already but not that one yet. Also going through the unity courses atm but still some stuff is like "what you mean?" :D

    Im not 20 anymore where everything goes bang bang bang into my head :D
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Couple extra points for you @Loykymar :

    I don't recommend using build index numbers. Yes it's legal. Yes a lot of people do it. BUT: if you ever change the order of your scenes you will NOT get an error and your game will cease to work, and you will be sad.

    Always use the scene's name. Copy/paste it out of the project window to avoid typos. And that way if you see it six months from now you'll understand it:

    Code (csharp):
    1. LoadScene( 2); // I have no idea what this scene number is
    2.  
    3. // vs
    4.  
    5. LoadScene ("mainmenu"); // oh I know what that is!
    Another point is if you just need some snippet of code to happen "a little bit later," I always use something like this:

    This is my CallAfterDelay class for delayed action.

    https://gist.github.com/kurtdekker/0da9a9721c15bd3af1d2ced0a367e24e

    See usage notes at bottom below gist code.
     
  7. Loykymar

    Loykymar

    Joined:
    May 23, 2021
    Posts:
    7
    @Kurt-Dekker Cheers, I will def use it for future refs(projects) but for the current project its easier as every scene has a name with the build no. e.g. Build no4 is scene no4 as its kind of story telling based (Text with images)

    As it is my very first project i just want to build something easy to get the feeling of all stuff (unity, coding, graphics, sounds, ect ect) and get to understand how everything works. As is stands atm i still cant get my head around what and how everything works , how does a int, bool, var, and so on works.

    Thanks again for all your help :)
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    You're very welcome!

    Let me offer you three broad buckets of knowledge to help sort out the firehose of knowledge you are soaking up:

    API buckets: you may find this helpful to organize your learning:

    - C# language syntax (organization, structure, grammar, punctuation.. the language)
    - the .NET API (all the tools that come with C#: lists, dictionaries, file IO, etc)
    - the Unity API (everything in the
    using UnityEngine;
    namespace)