Probably just some dumb minor mistake that I'm too stupid to see right now, but it really bothers me. I have this little game I'm working on in Unity and I wrote a method that is basically supposed to start the next level:
public void ButtonNextLevel()
{
string currentScene = Convert.ToString(SceneManager.GetActiveScene().name);
char[] currentSceneChars = new char[currentScene.Length];
int stringLength = currentScene.Length;
int levelIndex;
using (StringReader sr = new StringReader(currentScene))
{
sr.Read(currentSceneChars, 0, currentScene.Length - 2);
}
string levelIdentifier = new string(currentSceneChars);
Debug.Log(levelIdentifier);
if(levelIdentifier == "Level")
{
levelIndex = currentScene[stringLength - 1];
Debug.Log(levelIndex);
string nextSceneName = "Level " + (levelIndex + 1).ToString();
if(SceneManager.GetSceneByName(nextSceneName).IsValid())
{
SceneManager.LoadScene(nextSceneName);
}
else {GameWon();}
}
}
public void GameWon()
{
SceneManager.LoadScene("GameWon");
}
The main Problem seems to be the if(levelIdentifier == "Level") statement. If I get to the point where this script is executed, I do get a response from Debug.Log(levelIdentifier); (which clearly states "Level"), but not from Debug.Log(levelIndex);:
Debug Log
I don't understand why the if-statement isn't executed even though the requirements are met.