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

Question Need help with scene management.

Discussion in 'Scripting' started by AmateurPrograming, Feb 26, 2021.

  1. AmateurPrograming

    AmateurPrograming

    Joined:
    Dec 27, 2020
    Posts:
    5
    When I pickup an object, that object is destroyed. When I enter another scene then come back to the previous one, the object is still there. I worked around this by detecting whether the object has been collected and destroying it if it has. However this isn't a foolproof solution, if you place down the object in a different scene, then come back to the scene that contained the object originally, it is there.

    This isn't like storing a player's health across multiple scenes, this is about an actual object. Maybe I should just use one scene for both levels?

    I am not loading the scene additively. I am also using a singleton player.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Nope, you pretty much have it. You need to store the state of something. Either you're destroying it because it's been collected/exist elsewhere, whatever it is. Or, you spawn it in if it hasn't been collected/placed elsewhere.

    Depending on your game, you might not be able to use just one scene. It really is just about tracking it on your own and saving the state of things as you need to. There are different ways of doing this and how extensive it needs to be depends on what you need. For you, you might need to know where the player placed the object and it what scene, so just save that data and check against it.
     
    AmateurPrograming likes this.
  3. AmateurPrograming

    AmateurPrograming

    Joined:
    Dec 27, 2020
    Posts:
    5
    I see, however how would I check which scene the object is placed? Would I just store the current scene that the object is being placed?
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Yep. Could just be the scene name, scene index, or what I would probably use is have a script with a your own designated scene ID in it that you don't change. Just in case you change your scene name or the order of the scenes. That way you just compare the custom scene ID in your script and see if you need to spawn the item there or remove it. Whatever works best.

    Just how I might do it if I was going that route.