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

Bug Instantiated objects disappear when changing scenes

Discussion in 'Scripting' started by Protvod, Aug 17, 2023.

  1. Protvod

    Protvod

    Joined:
    Feb 21, 2020
    Posts:
    8
    In my game, when clicking a button, you go to a scene where you select the object you wan't to place, then you return to the game scene to place the object. This all works fine, but when I wan't to place another object and thus go to the object selection scene, then return to the game scene, the first object I placed is gone.
    The script that instantiates the objects is on an empty object, which makes this object the parents of all placed objects.
    I tried DontDestroyOnLoad in the awake method of that script, which worked, but this made it so that other methods in the scripts weren't being called. So I wan't to try an approach without this DontDestroyOnLoad method, can anyone give me some solution?

    I'd send my script, but it looks more like spaghetti than code.

    (Also I've set this as a bug, but I don't think it's really a bug and this is what the editor intends to do)
     
  2. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    I'm not sure what you mean by this, unless you mean that the original references where also deleted, and were re-created during scene reload, therefor having different pointers(Instance ID's). So I would just use a
    if (object == null) {re-get object}
    .

    I also recall a way to do additive scenes, so that the main scene isn't deleted, it just get's put on hold while in the other scene.

    And if neither of those ideas help, you'd have to do a complicated setup of a static class, with a static List of instances of each class you wish to keep in-game regardless. Then have some sort of method that checks through that List, and if it's gameObject == null, then re-create it's prefab. Never played around with this yet, it's just an idea I came up with.

    But for all intents and purposes, DontDestroyOnLoad should work just fine.
     
  3. Protvod

    Protvod

    Joined:
    Feb 21, 2020
    Posts:
    8
    I like this idea and tried it, but I still run into a problem: when loading the object selector screen, the main scenes stays active, which is great. But when loading the main scene after choosing the object to place, it closes all current scenes and reloads the the main scene. Is there any way I could load the already loaded main scene without it reloading?
    The answer probably is obvious, but I can't find it
     
  4. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
  5. Protvod

    Protvod

    Joined:
    Feb 21, 2020
    Posts:
    8
    After some research I've found there to be two LoadSceneMode's, one is Additive and one is Single.
    If I use Additive, two scenes will be opened at once, but only one will be visible, the object selection scene. But afterwards I don't know what mode I have to use to go back. If I use Additive, the game scene will be opened twice and if I use Single, all scenes will close and the game scene will be reloaded.
    This means that both won't work for returning to the game scene. Which makes me believe that their should be another mode, but if you look at this documentation: https://docs.unity3d.com/ScriptReference/SceneManagement.LoadSceneMode.html you'll see that there are only the Single and the Additive mode. There must be something I'm missing.
     
  6. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
  7. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    You can have multiple scenes loaded and view them together.

    You just unload the scene you want to remove.

    Though this doesn't seem like a situation where additive scenes are the tool to be used.

    Is this just UI stuff?
     
    wideeyenow_unity likes this.
  8. Protvod

    Protvod

    Joined:
    Feb 21, 2020
    Posts:
    8
    Unloading the object selection scene did it. Thanks a lot! I did have to replace the Start() method since that didn't work anymore, but it all works now.
     
    wideeyenow_unity and spiney199 like this.