My if-statement doesnt work. What did I do wrong?

Randomdud3

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.

Guru Stron

I do get a response from Debug.Log(levelIdentifier); (which clearly states "Level")

Are you sure that there is no whitespace characters in the debug output (like space or something)? Try adding Debug.Log(levelIdentifier.Length); also.

As for if statement it seems like a very convoluted way to check if string starts with some prefix. Since Scene.name is already a string try using stringStartsWith (another potentially useful one is string.Contains):

var sceneName = SceneManager.GetActiveScene().name;
if(sceneName.StartsWith("Level"))
{
    ...
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

My fullscreen menu won't work. What did I do wrong?

My second carousel wont work. What did I do wrong?

program does not work, what did I do wrong?

What did I do wrong in my dual drive partitioning?

My quickSort is not working correctly. What did I do wrong?

What did I do wrong with my 301 redirects?

What did I do wrong with mxGetPr

What did i do wrong in this JavaScript code

I have tried grub booting to reset my password but it won't work, did I do something wrong?

My php/mysql register form doesnt work write. What do i do?

regex or does not work - I do not know what is wrong in my pattern

Gettext doesnt work what I'm doing wrong?

Timer does not stop with my if statement, did I place in in the wrong part?

My string check doesnt work and I dont know what is the most eficient method to do it

Simple WebRTC Example! But why it didn't work & what did I do wrong?

My rust code compiled to wasm is slower than js, what did i do wrong?

what's wrong with this include statement that i did in php?

pgSQL order by desc not work correctly or what I did is wrong?

What did I do wrong when I installed this theme?

What is wrong with my if statement?

What is wrong with my statement

I created a class to return a confidence interval after bootstrapping, but my confidence interval looks oddly narrow. What did I do wrong?

My page goes blank when I call Router from "react-router-dom". What did I do wrong?

It always says Correct! What did I do wrong?

What did I do wrong in OOP in C++

While (true) problems, what did i do wrong?

Project Euler: Largest palindrome (Python). What did I do wrong?

What did I do wrong ....? ( calculating the amount of cupcakes per person )

What did I do wrong? UPDATE with condition and LIKE postgres