Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.

    Read our full announcement for more information and let us know if you have any questions.

Question Newbie question - multiple scenes loaded in at once

Discussion in 'Scripting' started by Lophane, Nov 23, 2022.

  1. Lophane


    Nov 15, 2022
    Hi there, I’ve been interested in coding for several years and I’ve learned a few rudimentary languages, just started trying out Unity and C# last week and I’ve been following a tutorial to learn the basics, though I have been making changes to make it more my own. I’m usually pretty good at finding answers to my questions with coding, but this one for some reason has me stumped and I think it’s more of a unity thing than a coding thing.
    My issue is that I finally got to the point where I can start adding in new levels and messing around with that, added in a level complete screen with code telling the scene manager to load the next scene when an input is given, but now that that is there and I’ve duplicated my first scene to have something to load from then on the first time I load up the game every scene loads in at once, only when the player collides with an obstacle and it reloads the scene does it start working normally, from that point on if I fail or succeed it behaves as I want it to and even goes to the next scene with no issues if you beat the first scene.

    Unfortunately I’m not at my computer anymore to actually grab my code but I’m not sure it’s an issue with the code in the first place, any help would be appreciated, thanks!
  2. Lophane


    Nov 15, 2022
    Not sure how clear it was but when I click play there are 2 of every item in the world at once and anything with a RigidBody immediately separates and goes flying
  3. Strafe_m


    Oct 24, 2022
    Is that the problem? If so, that's just your RigidBody properties, you can always set constrains for position and rotation, you can find that under 'constraints' on the RigidBody.
  4. Lophane


    Nov 15, 2022
    I mean isn’t it a problem? If I set limits to the obstacle’s RBs I won’t be able to have the player impact them a fling them around which I think is cool, and it’s not just duplicating physical objects, it’s duplicating everything, camera and lighting, etc.

    It’s making a bunch of errors pop up that there are multiple of things there shouldn’t be multiple of, audio listeners and the like, even if I remove the duplicate specific items and lock the rigidbody of each thing the player is also there and that needs to be moved around, if I were to remove the 2nd one then when I load into scene 2 there won’t be a player to use
  5. Kurt-Dekker


    Mar 16, 2013
    Sounds like you're doing something wrong additive-loading-wise. Time to debug it! (see bottom of this post)

    Here's some random notes on the process:

    Additive scene loading is one possible solution:

    A multi-scene loader thingy:

    My typical Scene Loader:

    Other notes on additive scene loading:

    Timing of scene loading:

    Also, if something exists only in one scene, DO NOT MAKE A PREFAB out of it. It's a waste of time and needlessly splits your work between two files, the prefab and the scene, leading to many possible errors and edge cases.

    Two similar examples of checking if everything is ready to go:

    To get started on debugging:

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

    Once you understand what the problem is, you may begin to reason about a solution to the problem.

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

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as

    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: or this answer for Android:

    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:

    When in doubt, print it out!(tm)

    Note: the
    function is an alias for Debug.Log() provided by the MonoBehaviour class.
  6. dlorre


    Apr 12, 2020
    You can indeed have multiple scenes loaded if you use the SceneMode.Additive parameter.

    You can keep track of what's going on with these:

    Code (csharp):
    2.         protected override void OnEnable()
    3.         {
    4. // other stuff...
    5.             SceneManager.sceneLoaded += LoadScene;
    6.             SceneManager.sceneUnloaded += UnloadScene;
  7. Lophane


    Nov 15, 2022
    Thanks for the advice!
    I did actually figure it out in the middle of setting up the debugging process, looks like I misfollowed the tutorial and I just don't know unity well enough yet, I only had the one scene so when I had the 2nd scene also up in the hierarchy it didn't strike me as odd.
    I did however use the debug method to figure out another issue I was having, and I'm sure the more I use it the more I'll just naturally add it in, thanks again!
    Kurt-Dekker likes this.