我试图在每次调用LevelChange函数时将1添加到变量levelname中。但是,每次调用它时,都会像在代码开头最初设置的那样将值重置为1。我习惯了c ++,非常困惑。这段代码有点草率,因为我尝试了很多方法来解决这个问题。任何帮助,将不胜感激。
缺少括号,因为某些原因我无法在代码块中找到此行。

public class NextLevel : MonoBehaviour {
    int levelname = 1;
    int newlevelname;
    string levelnameS;

   void LevelChange()
   {
       levelname++;
       newlevelname = levelname;
       string levelnameS = newlevelname.ToString(); //Converts newlevelname which is an int value to a string
       Debug.Log(levelnameS); //prints to the console

       SceneManager.LoadScene(levelnameS); //changes scene based on the level name. In this case its a number because thats what the levels are named.
       Debug.Log(levelname);
   }

   void Update()
   {
       if (Input.GetKeyDown(KeyCode.Return))
       {
           LevelChange(); //calls the level change function
       }
   }
}

最佳答案

使用public static int levelname = 1;
代替int levelname=1

关于c# -++运算符问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39304967/

10-09 05:31
查看更多