如何获取和比较当前场景名称

时间:2016-11-16 01:32:50

标签: c# unity3d

我尝试比较场景名称,但我收到以下错误:

  

错误CS0019:操作员==' cannot be applied to operands of type 方法组'和`string'

我无法弄清楚如何修复此错误。请帮我!错误位于箭头

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

2 个答案:

答案 0 :(得分:1)

SceneManager.LoadScene是用于加载场景的void函数。它不会返回任何内容,因此您无法将其与字符串进行比较。

您似乎想要将当前场景name"Level" + i进行比较。如果这是真的那么你正在寻找SceneManager.GetActiveScene().name

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

答案 1 :(得分:0)

请记住==运算符检查两个操作数的值是否相等,这取决于两个操作数应该是相同类型的条件。从错误消息中可以清楚地看出LoadScene被定义为一种方法。

如果您使用这样的代码,您的代码将正常工作:

if (SceneManager.LoadScene(params if any) == "Level" + i)
{
    // Code here 
}

符合 LoadScene()方法应返回字符串对象的条件,否则返回类型将是业务对象,在这种情况下使用字符串属性进行比较

相关问题