IF statement isn't executing even though the condition is met

Hi all,

I’m creating OnLevelWasLoaded() method to check the name of the scene and if it is the scene in question that it executes some code. I’ve checked with debug statements to make sure it is but the if statement isn’t met. I’ve tried it with an ||(OR) condition as a test then it worked.It isn’t making any sense to me why this if statement isn’t being met. It is the IF statement checking the Scene name that I’m having problems with.

 void OnLevelWasLoaded()
    {
        Scene currentScene = SceneManager.GetActiveScene();
        string sceneName = currentScene.name;
        Debug.Log("Scene name = "+sceneName);
        Debug.Log("Current scene = "+ SceneManager.GetActiveScene().name);
      if (sceneName == "01_level_01")

        {
            Debug.Log(sceneName);
            if (gameStart)
            {
                PlayerScore.scoreCount = 0;
                PlayerScore.CoinCount = 0;
                PlayerScore.LifeCount = 2;

                GameController.instance.SetScore(0);
                GameController.instance.SetCoinScore(0);
                GameController.instance.SetLifes(2);
            }
        }
    }

What is the scene name that’s logged? Don’t type it here, cut and paste it. Are either of those zeros actually the letter “O”? Any spaces? What about case?

Instead of sceneName == “01_level_01” try sceneName.Equals(“01_level_01”)

01_level_01

Hi David I’ve checked many times and copied the name from the scenes name itself from the inspector.

Hi Dameon, thanks for the suggestion just tried it still isn’t working.

If it’s not working, then something is off. Try seeing if sceneName.Contains(“01_level_01”) works. Are you certain that it’s not proceeding past the if statement, and gameStart is set to true?

You may also consider trying the index of the scene as a test. But what you may also consider doing is a char comparison of the scene name and the string. This way you can see if each char is actually matching up. If everything is true, then you know something else is wrong. If something returns false, you know what char doesn’t match.

The .Contains will also help show this. Or you can throw your returned scene name and the string you are checking into a a string comparison online. Also note, make sure your scene name doesn’t contain spaces at the start or end that you may not notice as that will throw it off.

Also OnLevelWasLoaded is a depecrated function as of 5.4. Are you sure its being called. Do these two lines actually fire (The ones before the if statement?)

 Debug.Log("Scene name = "+sceneName);
        Debug.Log("Current scene = "+ SceneManager.GetActiveScene().name);

I’ve just tried using contains and is still returning false

Hi Branthnann thanks for the response. Not sure how to do a char comparison I’ve looked but haven’t found suitable way in unity to do it, also unaware of online tools to compare strings would be interested in knowing how. Although all the debuglogs W are outputting the correct name and I didnt manually type it myself I copied and pasted it straight from the scenes filename itself.

Even stranger is that this is from a Udemy tutorial I was following side by side and then recreating myself so memorize how to create it myself. The code I’m using other than trying
SceneName.contains is exactly the same, I’ve also tried the same build order. It works on the original I did aslong side the tutorial but doesn’t on the one I’m recreating myself.

Hi Takatok thanks for the response, I’m using ver 5.4, as mentioned above the code works fine in the version I created aslong side the udemy tutorial.

I’ve done other tests and the OnLevelWasLoaded works.

This is why I mentioned you might have a space at the end or something.
String comparisons can be find online by searching for string comparision online.

        string string1 = "01_level_01";
        string string2 = "O1_level_O1";
        Debug.Log("string1 length " + string1.Length);
        Debug.Log("string2 length " + string2.Length);
        for(int x = 0; x < string1.Length;x++)
        {
            if (string1[x] == string2[x])
                Debug.Log("Index " + x + " is true");
            else
                Debug.Log("Index " + x + " is false");
        }

This code will print out the lengths as well as loop through and compare each char. Just replace the values of the strings so that one is your scene name and one is the copied version of the typed string you are comparing against in your script. In this script. I’ll get 2 false as I put 0 in one string and O in the other.

1 Like

Hi thanks Brathnann
A friend of mine that programmes and game designs professional had a quick look and he noticed that it was outputting level as “Level” with a capital.

He was unable to figure out why but he assumes for some reason the way unity processes a word after a _makes the string begin with a capital. At least I realize now for the future. I renamed the scene name and if statement to using a capital L and now works perfectly.

I was told to quit using underscores for stuff a while ago and the people who told me couldn’t give me a reason, but I just deferred to their judgement since they’ve used the engine for like 5 years.

I guess there you go!

1 Like

Typically, the reason is because in C++, variable names starting with underscores are reserved, so a lot of programmers carry that convention with them to other languages. C# has no problem with underscores. This appears to be a problem with a Unity bug, rather than a generalized problem with underscores, so I wouldn’t necessarily take it as a reason not to use underscores.

Still, they should probably be avoided.

2 Likes

To be frank though I think they emphasize the relationship between fields and parameters which is good. People are likely to just overlook that entirely if they just start wiring stuff up themselves without programming experience and doing what works.

Since I’m the only one working on this project I don’t always worry about encapsulation too much, but I have for sure already found some places where if I didn’t use backing fields I could screw up my behaviors by changing the inspector properties and not noticing.