Delay next scene with scenemanager script

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

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


public class ChangePage : MonoBehaviour
{
   public void SceneLoader(int SceneIndex)
    {
        SceneManager.LoadScene(SceneIndex);
    }

SceneIndex will not work somehow (I can choose which scene but only in script and that’s not what I want)

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

public class LoadLevelAfterTime : MonoBehaviour
{
    public void ButtonFunction()
    {
        StartCoroutine(DelaySceneLoad());
    }

    IEnumerator DelaySceneLoad()
    {
        yield return new WaitForSeconds(1f); // Wait 1 seconds
        SceneManager.LoadScene("Scene1");
    }

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

make sure you have your scenes added to the BuildSetting

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 :

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

public class LoadLevelAfterTime : MonoBehaviour
{
    public void ButtonFunction()
    {
        StartCoroutine(DelaySceneLoad());
    }

    IEnumerator DelaySceneLoad()
    {
        yield return new WaitForSeconds(1f); // Wait 1 seconds
    }

    public void SceneLoader(int SceneIndex)
    {
        SceneManager.LoadScene(SceneIndex);
    }
}

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

you dont need to try oder way around yet, what you need is C# for beginner starting from the basic

public class LoadLevelAfterTime : MonoBehaviour
{
    public void ButtonFunction(int SceneIndex)
    {
        StartCoroutine(DelaySceneLoad(SceneIndex));
    }

    IEnumerator DelaySceneLoad(int SceneIndex)
    {
        yield return new WaitForSeconds(3f); // Wait 1 seconds

        SceneManager.LoadScene(SceneIndex);
    }

    public void SceneLoader(int SceneIndex)
    {
        SceneManager.LoadScene(SceneIndex);
    }
}

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

https://www.youtube.com/watch?v=IFayQioG71A

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?” :smile:

Im not 20 anymore where everything goes bang bang bang into my head :smile:

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:

LoadScene( 2); // I have no idea what this scene number is

// vs

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.

See usage notes at bottom below gist code.

@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 :slight_smile:

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)