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.

Storing an exact object and recalling it in later scenes

Discussion in 'Scripting' started by dhaussler, Oct 26, 2017.

  1. dhaussler

    dhaussler

    Joined:
    Oct 26, 2017
    Posts:
    3
    I'm looking at a way to have several objects that are falling/spinning/flying and are all "stored" away once they enter a specific space. The player no longer interacts with them for the rest of the scene.

    In the following scene the player may on-command recreate these objects with the exact same physical properties as before (velocity, rotation, drag, etc). They at least need to have the same rigidbody parameters, but ideally would actually be a new instance/clone of the exact same object with all the exact same components as well.

    The best way I can think of is to create prefabs (which will limit what objects can be stored) and then save every value associated with the Rigidbody component. If there are multiple objects I'm at a loss as to the best way to store and access all these values when the new objects are instantiated in the next scene.

    Anyone have advice on a way to approach this? I have been searching and come up a bit short.
     
  2. MaxGuernseyIII

    MaxGuernseyIII

    Joined:
    Aug 23, 2015
    Posts:
    315
    What about DontDestroyOnLoad?
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    37,264
    Does it have to be a new instance? Why don't you just keep track of the GameObjects involved (just the uppermost one obviously), then do a .SetActive(false) on all of them and mark them as DontDestroyOnLoad().

    Then in your next scene, they will all be present, they will all be invisible and inactive, and you can switch them back on when you want to. You will still need to be sure to manually destroy them now however, as the DDOL will persist.
     
  4. dhaussler

    dhaussler

    Joined:
    Oct 26, 2017
    Posts:
    3
    I knew I would have to use "DontDestroyOnLoad", but didn't think about just setting the whole object as inactive. This will work much better. I wonder if the physical properties persist once set to active again or if they zero out. I guess I'll do some experimenting.

    thanks for the help!
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    37,264
    If you mean like the velocity and spin of a Rigidbody, pretty sure those will persist, but definitely do some experimenting.

    Hope it works out the way you need it to!
     
  6. iamvideep

    iamvideep

    Joined:
    Oct 27, 2017
    Posts:
    118
    I think the properties should persist. If they dont, maybe you can go to saving states in an xml or json.

    You can write a class or function to handle these objects. All you need to do is to write the rigid body component values and whatver you want and keep it aside.
    Also, setActive(false) the gameObject.
    Once you reload the scene or get back then you can enable the objects by SetActive(true) and then set the properties.

    This looks intensive but can be a good way to handle the properties and values if they are lost.

    Let me know if it works.

    Cheers!