Integer bug with global script

I’m currently working on my game and want to make a back button that retirning the player to the map of the current world.

Now, I’m currently having problems with my global script.

The button has being set a function inside the script to return to the map of the current world, but the function will bring the player to the map of the first world REGARDLESS of the current world. Or should I say, the ToString function for the current world number was tripped to 1 during the function.

Visualization:
Currently in World 1:

  • Enter a stage
  • Press back
  • Entered World 1 map

Currently in World 2:

  • Enter a stage
  • Press back
  • Entered World 1 map?!

Is this a bug or what?

Its likely a bug in your code. Smaller chance its just you misunderstanding how a particular API works. Impossible to say either way without seeing code. Make sure to use code tags if you post any.

A portion of my global script, showing the “world” integer and the function to return to the stage map.

public int world = 1;

~~~~~~~~~~~~~~~~~~~~~

public void BackToStageSelect()

{
ClickSound();
SceneManager.LoadScene("World" + world.ToString());
}

The “world” integer variable can be changed through the script itself.
However, the function seems to trip and returned 1 instead of its own current value (in my case, 2)

You have a bug in your code somewhere, but the code you posted isn’t nearly enough to determine what the problem is. Unity has no bugs with basic math/logic, otherwise no games could ever be made with it. It would be a good idea to learn debugging techniques, even simple ones like printing the value of variables at pertinent points so you can see what the value actually is, as opposed to what you assume it is.

–Eric

Try this:

public void BackToStageSelect()
{
ClickSound();
string nextScene = "World" + world.ToString();
Debug.Log("Changing Scene to: " + nextScene);
SceneManager.LoadScene(nextScene);
}

See what comes out on the Console. Maybe world isn’t being set correctly even if you think it is. However, if there was any case of there being a chance that Unity’s side had a bug its with the LoadScene and strings instead of buildindex numbers. I’ve seen a lot of posts of people having problems with it.

Also check that World2 is actually the scene you think it is in the buildindex.

From console:
World1

In the global script in the inspector:
World: 2

So possibly you have another world variable somewhere. If there is a world variable in local scope it will mask your global variable. I assume when you say global variable you mean a global variable in this script not in another script? Global variables are not project wide, just in the script itself.

Just done a workaround with this by redirecting the variable to another script.

Now it works.