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. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Question Newbie question - multiple scenes loaded in at once

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

  1. Lophane

    Lophane

    Joined:
    Nov 15, 2022
    Posts:
    6
    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

    Lophane

    Joined:
    Nov 15, 2022
    Posts:
    6
    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

    Strafe_m

    Joined:
    Oct 24, 2022
    Posts:
    71
    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

    Lophane

    Joined:
    Nov 15, 2022
    Posts:
    6
    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

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    29,825
    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:

    https://forum.unity.com/threads/right-way-for-performance-divide-scene.1023673/#post-6630961
    https://forum.unity.com/threads/right-way-for-performance-divide-scene.1023673/#post-6754330

    https://forum.unity.com/threads/problem-with-canvas-ui-prefabs.1039075/#post-6726169

    A multi-scene loader thingy:

    https://pastebin.com/Vecczt5Q

    My typical Scene Loader:

    https://gist.github.com/kurtdekker/862da3bc22ee13aff61a7606ece6fdd3

    Other notes on additive scene loading:

    https://forum.unity.com/threads/removing-duplicates-on-load-scene.956568/#post-6233406

    Timing of scene loading:

    https://forum.unity.com/threads/fun...ject-in-the-second-scene.993141/#post-6449718

    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:

    https://forum.unity.com/threads/daily-events-and-content-changes.1108202/#post-7143713

    https://forum.unity.com/threads/uni...on-before-other-scripts.1153739/#post-7401794

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

    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
    Debug.Log("Problem!",this);


    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

    When in doubt, print it out!(tm)

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

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    308
    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):
    1.  
    2.         protected override void OnEnable()
    3.         {
    4. // other stuff...
    5.             SceneManager.sceneLoaded += LoadScene;
    6.             SceneManager.sceneUnloaded += UnloadScene;
    7.  
     
  7. Lophane

    Lophane

    Joined:
    Nov 15, 2022
    Posts:
    6
    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.