Creating a Reusable Scene Change Function in Unity

Unity

Creating a Reusable Scene Change Function in Unity

Hello friends, in this article I will explain how you can change scenes everywhere with a single function instead of writing code repeatedly when developing games with Unity.
As you know, the first rule of software is the do not repeat yourself rule. That's why, thanks to the scene change code I will show you now, you won’t have to write the same scene loading code repeatedly for different scenes when coding your game, thus avoiding repetition.

First, let's create a script called "SahneDegistir" and then we write our code as follows.

using UnityEngine;
using UnityEngine.SceneManagement;
public class SahneDegistir : MonoBehaviour
{
    
    public void SahneDegistirr(int gidilecekSahne)
    {
        SceneManager.LoadScene(gidilecekSahne, LoadSceneMode.Single);
    }
}

The logic of the code here is actually very simple, and generally anyone who deals with software will use something like this, but I wanted to focus a little for those who don’t pay attention or aren’t aware.

As for the usage, you can now use this script in any scene you want and it is quite simple.
In Unity, in the scene you want to use, select "Create Empty" in the Hierarchy, name it "SahneDegistir" or whatever you want, then drag the script you wrote onto it and you can use this SahneDegistir by dragging it from the Hierarchy to the buttons.

Hierarchy


We select "Create Empty" in the Hierarchy and name it, I named it "SahneDegistir".

script

We drag and drop the script into the object we have created.

Button

For our button, first press + and in the newly created section, drag the object you created in the hierarchy to the none area, then from the section next to Runtime, select our script and from there select the SahneDegistir function. In the box that appears below, write the number of the scene you want to go to and it will go to that scene.
In this way, with the code we use, we can change our scene by accessing the code in this script without writing scene change code again and again.
If we want to access this script from another script, let’s write a simple code for it and see how it’s done.

using UnityEngine;
public class Script2 : MonoBehaviour
{
    public GameObject sahneDegistirScript;
    void SahneDegistirmeyeEris()
    {
        var sahneDegistir = sahneDegistirScript.GetComponent<SahneDegistir>();
        sahneDegistir.SahneDegistirr(5);
    }
}

What have we done in the code above? First, we created a public GameObject variable and will use it to pull the sahnedeğiştir object. Then, with the function we created—note that you don’t necessarily have to create a function, the important part is the var sahneDegistir—we accessed the SahneDegistir script among the components of the object we pulled with the public GameObject and then accessed the function we wrote to change the scene and entered the scene number we wanted to go to.

Get Script

The reusable scene change function is that simple.

If you want to take a look at the sample project I made, you can click here and review it on GitHub.