Today we’re going to talk about one of the basic concepts of Unity 3D, software that allows you to create video games.
We’re going to see how to change scenes.
It’s really simple. First, you need to create a C# script, which I’ll call for example ChangeScene.cs.
The first thing to do is import the SceneManagement class:
using UnityEngine.SceneManagement;
After that, we start building our script by creating a variable:
public string LevelToLoad;
We’re almost done. What remains is to write a function that loads the content of our variable:
void LoadLevel()
{
SceneManager.LoadScene(LevelToLoad);
}Once done, you’ll need to type the name of the scene to change in the empty field of the script that you’ll attach to the GameObject, in my case “MainMenu”.

What remains is to trigger the event in the animation by going to “Animation” and clicking on the small white “Add Event” button.

If, on the other hand, you’d prefer to put the scene name directly in your script, you could do it this way:
void LoadMenu () {
SceneManager.LoadScene("MainMenu");
}Obviously MainMenu is the name of an already created scene.
Here is the complete script:
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class ChangeScene : MonoBehaviour {
public string LevelToLoad;
void LoadMenu () {
SceneManager.LoadScene("MainMenu");
}
void LoadLevel()
{
SceneManager.LoadScene(LevelToLoad);
}
}IMPORTANT: for this to work, your scene must be present in the Build Settings of your game.

Your turn to play!







0 Comments