user3103529 user3103529 - 11 months ago 63
C# Question

Issue with ++ operator

I'm attempting to add 1 to the variable levelname everytime the LevelChange function is called. However, everytime its called it resets the value to 1 like its originally set in the beginning of the code. I'm used to c++ and very confused. This code is a bit sloppy because ive tried a ton of ways to solve this. Any help would be appreciated.
Theres a bracket missing because for some reason i cant get this line to go in the code block.

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

void LevelChange()
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.

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

Answer Source

use public static int levelname = 1; instead of int levelname=1