Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Scenes not loading in the right way

Discussion in 'Scripting' started by MrButterPants, Apr 29, 2019.

  1. MrButterPants

    MrButterPants

    Joined:
    Apr 1, 2019
    Posts:
    17
    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 Screenshot (1).png Capture.PNG
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Put a unique Debug.Log() call in your code right next to each SceneManager.LoadScene call.
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    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.
     
    MrButterPants likes this.
  4. MrButterPants

    MrButterPants

    Joined:
    Apr 1, 2019
    Posts:
    17
    Screenshot (2).png Capture.PNG Capture2.PNG Capture3.PNG Capture4.PNG
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    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.
     
    MrButterPants likes this.
  6. MrButterPants

    MrButterPants

    Joined:
    Apr 1, 2019
    Posts:
    17
    I've not been able to find what "string" function/code you're talking about sadly
     
  7. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    SceneManager.LoadScene("your scene name here");
     
    Joe-Censored likes this.
  8. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    It's named the same as the number one, but it just takes a string instead.
     
    MrButterPants and Joe-Censored like this.
  10. MrButterPants

    MrButterPants

    Joined:
    Apr 1, 2019
    Posts:
    17
    thank you all,i finally got it working and it's made things easier,thank you!
     
    Kurt-Dekker likes this.
  11. devinmcphee

    devinmcphee

    Joined:
    Oct 22, 2015
    Posts:
    4
    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?
     
  12. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Yes. You are necroposting.

    Necroposting:


    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://forum.unity.com/threads/using-code-tags-properly.143875/

    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://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    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
     
  13. CassClay

    CassClay

    Joined:
    Mar 21, 2020
    Posts:
    20
    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!