Scenes not loading in the right way

I have everything how i need it,but when i try and load my scenes,they swap scenes even though i have specific numbers listed

for example,when i click my “grenade western” button to go to my western level,it loads the ending screen instead,even though grenade western is scene 6 and the script for it says to load scene 6

what’s happening?,i included some screenshots in case anyone gets confused

Put a unique Debug.Log() call in your code right next to each SceneManager.LoadScene call.

Personally, I prefer using the string version of LoadScene, as without just knowing or looking at the build settings, the index number doesn’t really mean anything.

Also, would help to see how you have it connected to your button to make sure it’s hooked in correctly.

1 Like

4486456--413188--Screenshot (2).jpg 4482811--412618--Capture.PNG 4486456--413194--Capture2.PNG 4486456--413197--Capture3.PNG 4486456--413200--Capture4.PNG

Are there any errors in your console preventing a recompile?

As already mentioned, just use strings to refer to the scenes. This prevents the wrong scene from being loaded, and is much easier to read your code to see what is being loaded.

1 Like

I’ve not been able to find what “string” function/code you’re talking about sadly

SceneManager.LoadScene("your scene name here");

1 Like
2 Likes

It’s named the same as the number one, but it just takes a string instead.

2 Likes

thank you all,i finally got it working and it’s made things easier,thank you!

1 Like

Sorry for resurrecting an old thread, but maybe someone can help me out…

I’m pretty new to Unity, and I’m working on a prototype that involves switching back and forth between different scenes.

I’ve got a trigger prefab with a scene switch script, using SceneManagement. It has a public field (I’ve tried both int and string) with the target scene.

If I step on the trigger, I get an ‘E’ prompt, and when I press E, I jump to the target scene. However, if I have two instances of the same prefab, even with different targets in each public field, both triggers lead to the same scene.

So say I’m in “Scene B”, and I put two instances of the prefab. One has the target “Scene A” and one has the target “Scene C”. But both trigger boxes lead to “Scene A”

For what it’s worth, I’ve got a Debug.Log("Player on Switch to " + sceneName); and it displays the correct scene name. But when I hit E, it sends me to the wrong scene.

Any idea what I’m doing wrong?

Yes. You are necroposting.

Necroposting:
https://www.youtube.com/watch?v=1kUlD7OWpLM

Start your own post. It’s FREE.

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • links to documentation you used to cross-check your work (CRITICAL!!!)
  • what actually happened, especially any errors you see

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379

You must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://discussions.unity.com/t/700551 or this answer for Android: https://discussions.unity.com/t/699654

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

If it’s still in someone’s use My Own problem with loading scene was as follow:
I had just one Script, that loads all the scenes, trough one method by name, when I made different method for each scene again called by name the problem was gone, could be a problem with older version, because I am using 2020’s. But could be something out of sync. However, placing each scene call in it’s own function, solved it!