Kirʞ DG Kirʞ DG - 19 days ago 6
C# Question

How to get and compare current scene name

I tried to compare scene name but I am getting the error below:


error CS0019: Operator
==' cannot be applied to operands of type
method group' and `string'


I can't figure out how to fix this error. please help me! the error is located on the arrow

void CheckCurrentLevel()
{
for(int i = 1; i < LevelAmount; i++)
{
---> if (SceneManager.LoadScene == "Level" + i) {
CurrentLevel = i;
SaveMyGame ();
}
}
}

Answer

SceneManager.LoadScene is a void function used to load a scene. It does not return anything so you can't compare it with a string.

It looks like you want to compare the current scene name with "Level" + i. If that's true then you are looking for SceneManager.GetActiveScene().name

void CheckCurrentLevel()
{
    for (int i = 1; i < LevelAmount; i++)
    {
        if (SceneManager.GetActiveScene().name == "Level" + i)
        {
            CurrentLevel = i;
            SaveMyGame();
        }
    }
}
Comments