What is Unity scene.buildIndex?


Hello friends, in this post I will talk about a code related to SceneManagement in Unity, our code is "buildIndex".
Now let's see together what this code does.

As you know, in Unity we add scenes from the "Build Settings" part by clicking "Add Open Scene" and numbers like 0, 1, 2 appear next to the added scenes starting from 0. The buildIndex command lets us access this number.

It gives us the index number of the current scene.
Let's examine this with some example codes.


First, we add our current scene from the "Build Settings" section.
Now let's write our codes to access the index number of the current scene.

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

public class buildIndex : MonoBehaviour
{   
    void Start()
    {
        Debug.Log(SceneManager.GetActiveScene().buildIndex);
    }
}


If we wrote the codes correctly, we should see an image like this in the Console.


The important point here is this: generally, we use this code to get the number of the current and active scene, so if we try to use a syntax like SceneManager.buildIndex without writing "SceneManager.GetActiveScene()", we will get an error.