Search Unity

Saving dontdestroyonload object

Discussion in '2D' started by YoYoYoYo_111_PiPi, Oct 25, 2016.

  1. YoYoYoYo_111_PiPi

    YoYoYoYo_111_PiPi

    Joined:
    Jun 23, 2016
    Posts:
    80
    Any good advice about saving the objects that we are carrying between scenes? So if we have an object in the starting scene, and then we move to other scenes, when we save progress then, how do we tell it to store that dontdestroyonload object. Cause my logic is it will load from that scene where we have saved, and this object is not originally from there... so it won't automatically load it. I've also am wondering how to tell it to save and load from a particular scene, but I guess that can be another thread :).
     
  2. romatallinn

    romatallinn

    Joined:
    Dec 26, 2015
    Posts:
    161
    You can make a prefab for this object. Then, in the scene you must check if the object exists. If it doesn't, instantiate its prefab. Of course, if there are some unique parameters in the prefab, you have to save them before leaving the game session and put the back on load.
     
    YoYoYoYo_111_PiPi likes this.
  3. YoYoYoYo_111_PiPi

    YoYoYoYo_111_PiPi

    Joined:
    Jun 23, 2016
    Posts:
    80
    Oh, OK, that's interesting, I'll look into instantiating the prefabs. Thanks.
     
  4. YoYoYoYo_111_PiPi

    YoYoYoYo_111_PiPi

    Joined:
    Jun 23, 2016
    Posts:
    80
    But just, what do you mean by ''...put the back on load''?
     
    Last edited: Oct 28, 2016
  5. romatallinn

    romatallinn

    Joined:
    Dec 26, 2015
    Posts:
    161
    I meant put them back on load. Put unique properties of the object back when the game is loaded.
     
  6. YoYoYoYo_111_PiPi

    YoYoYoYo_111_PiPi

    Joined:
    Jun 23, 2016
    Posts:
    80
    Oh, thanx :D...
     
  7. YoYoYoYo_111_PiPi

    YoYoYoYo_111_PiPi

    Joined:
    Jun 23, 2016
    Posts:
    80
    So, this means that we can make changes to the prefab, and save those... all during gameplay?
     
  8. romatallinn

    romatallinn

    Joined:
    Dec 26, 2015
    Posts:
    161
    No, I do not think that it is possible to save the changes of the prefabs in such way that you will be able to instantiate it later with this data. However, you can save the data somewhere else: PlayerPrefs or XML. It depends on the type of data that the prefab contains. And, afterwards, when the prefab is instantiated, load this data and apply it to the object/prefab on the scene.
     
    YoYoYoYo_111_PiPi likes this.
  9. YoYoYoYo_111_PiPi

    YoYoYoYo_111_PiPi

    Joined:
    Jun 23, 2016
    Posts:
    80
    Oh, that makes sense... thanx for all the info, that was helpfull :D. If something else comes to mind, I'll ask you here again...
     
    romatallinn likes this.