C# script to Load a new scene from a Button click.

I am attempting to make a C# script that will initiate a scene change when a button is clicked. The MonoBehavior and SceneManager are not loading as classes.

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

public class LoadScene : MonoBehaviour
{

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

}

Neither the MonoBehavior or the SceneManager are appearing as classes. How do I fix this?

Hello there.

You are kinda lost i think…


First

you are doing this when define your fucntion:

public void LoadScene.SceneLoader(int SceneIndex)

LoadScene.SceneLoader should be the name of the function, so dont add points… just do this:

public void LoadScene(int SceneIndex)

Second, if your function have parameters inside the parentesis " ( ) ", you must gice that information when calling the function. As you pretend to call it from a buton click, you can not give that parameters info, so you need a function without parameters:

public void LoadScene()
{
 //the function things
}

Second

As you want to load a new scene, you need to define which scene you want to load.

So you will need to give the index number of the scene you want to load (the index number of the scenes can be seen in the Build Seting window). For example:

 SceneManager.LoadScene(2);

Third

Now only need to asign the function to the button. This can be done from the Inspector in the Editor, If don’t know how, look it in youtube, its quite simple.

IS this what was you looking for?

Bye!