Search Unity

Preserving the last sprite of an animation in a reload scene

Discussion in 'Scripting' started by kaitoren, Aug 28, 2018.

  1. kaitoren

    kaitoren

    Joined:
    Jul 7, 2018
    Posts:
    31
    I have an obstacle in my game that's driving me crazy. In the scene of my game I have a sprite animation in a canvas that changes using 'SetTrigger' when a button is pressed (the element is a building that tumbles down. 'standing building' or idle and 'collapsed building', two sprites). I would like to keep the last sprite animation ('collapsed building') referenced to the object after the scene is reloaded, but I don't know how. The object always returns to its default state ('standing building' sprite).

    I cannot used ActiveSceneChange because it's not a new scene, but the same scene reloaded, and, as a begginer, I don't know how to use properly the static method way.

    I'd really appreciate if someone could give me any ideas that can help me with this. Thanks!
     
    Last edited: Aug 28, 2018
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Saving & loading data is a rabbit hole that is very, very deep. There are many ways to save and load data to preserve the state of a scene or object, with varying advantages and disadvantages, and with varying degrees of scalability.

    Basically you have to store values somewhere, and then retrieve those values later. The scene itself can't change after you create it in the editor and build the game, so you need to use stored data to change it when it loads.

    One option which is probably the most straightforward is to use PlayerPrefs. This has the disadvantage of not being very scalable, but for small projects it can work just fine and it's easy to code without any setup on an object-by-object basis.

    Make sure your object has a unique name, so that it can tell its data apart from other objects that might have the same variable key.

    Then you can do something like this:
    Code (CSharp):
    1. public class SaveLoadExample : MonoBehaviour
    2. {
    3.     // here we use a constant value so that we avoid typos and know that it shouldn't change during the game
    4.     private const string COLLAPSED_KEY = "collapsed";
    5.     private bool collapsed;
    6.  
    7.     private void Awake()
    8.     {
    9.         Load();
    10.     }
    11.  
    12.     private void OnDestroy()
    13.     {
    14.         Save();
    15.     }
    16.  
    17.     private void Save()
    18.     {
    19.         // store an integer representing this object's collapsed boolean
    20.         int booleanAsInteger = collapsed ? 1 : 0;
    21.         PlayerPrefs.SetInt(gameObject.name + COLLAPSED_KEY, booleanAsInteger);
    22.      
    23.         // the saved data might look like: { "MyBuilding01collapsed", 0 }
    24.  
    25.         // save it to disk
    26.         PlayerPrefs.Save();
    27.     }
    28.  
    29.     private void Load()
    30.     {
    31.         // if this object's variable has a saved value, load it
    32.         if (PlayerPrefs.HasKey(gameObject.name + COLLAPSED_KEY))
    33.         {
    34.             // assign the variable with the stored value
    35.             int storedValue = PlayerPrefs.GetInt(gameObject.name + COLLAPSED_KEY);
    36.             bool intAsBoolean = storedValue == 1 ? true : false;
    37.             collapsed = intAsBoolean;
    38.         }
    39.     }
    40. }
    I haven't tested that but I'm pretty sure it would work, obviously you can adapt it to your own situation. There are many ways to improve this system, or make it more reusable, but for a beginner this is a good place to start.

    Note: PlayerPrefs doesn't accept booleans, but you can use an integer value of 1 or 0 like I did here.

    Note: this will save progress even if you restart your game, as it gets saved to disk. There are functions you can use like "PlayerPrefs.DeleteAll" to erase all the saved data. Or "DeleteKey" for just one value.

    https://docs.unity3d.com/ScriptReference/PlayerPrefs.html
     
    Last edited: Aug 28, 2018
    kaitoren likes this.
  3. kaitoren

    kaitoren

    Joined:
    Jul 7, 2018
    Posts:
    31
    Thank you very much for the info about the PlayerPrefs. Although I've decided that I'm not going to reload the scene, so, I'm not going to be much use of these Playerprefs for this matter. However, it will help me to try to make a scoreboard. :)
     
    LiterallyJeff likes this.