++运算符的问题

时间:2016-09-03 08:41:18

标签: c# unity3d syntax logic operators

每次调用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                  
       }
   }
}

2 个答案:

答案 0 :(得分:4)

int levelname=1是一个实例属性。类levelname的每个实例都有自己的值。因此,如果您每次创建NextLevel的新实例并且调用NextLevel计数始终从1开始。

您可以将Update切换为静态属性,也可以始终使用类levelname的一个实例。

答案 1 :(得分:2)

使用App\Console\Kernel 而不是public static int levelname = 1;

相关问题