Search Unity

Scriptable Object truncates on Unity Editor close/open

Discussion in 'Editor & General Support' started by roshaantariq, Dec 26, 2017.

  1. roshaantariq

    roshaantariq

    Joined:
    Dec 6, 2017
    Posts:
    32
    I'm have some issues regarding scriptable objects. I want to save player data into scriptable object for persistance. But when I restart i.e. close and then again open Unity Editor my scriptable Objects resets and flushes everything.

    These are my classes which I'm saving

    [System.Serializable]
    public class PlayerCard
    {
    public string bibleReference;
    public int bookNumber;
    public int chapterNumber;
    public int startingVerse;
    public int endingVerse;
    public int masteryOfVerse;
    public int timePracticed;
    public int timeUsedToHunt;

    public PlayerCard() { }

    public PlayerCard(string bibleReference, int bookNumber, int chapterNumber, int startingVerse, int endingVerse, int masteryOfVerse
    , int timePracticed, int timeUsedToHunt)
    {
    this.bibleReference = bibleReference;
    this.bookNumber = bookNumber;
    this.chapterNumber = chapterNumber;
    this.startingVerse = startingVerse;
    this.endingVerse = endingVerse;
    this.masteryOfVerse = masteryOfVerse;
    this.timePracticed = timePracticed;
    this.timeUsedToHunt = timeUsedToHunt;
    }
    }

    [System.Serializable]
    public class PlayerVerse
    {
    public string bibleReference;
    public int bookNumber;
    public int chapterNumber;
    public int verseNumber;
    public bool usedOnAPlayerCard;
    public int mastery;
    public int timePracticed;
    public int timeUsedToHunt;

    public PlayerVerse() { }

    public PlayerVerse(string bibleReference, int bookNumber, int chapterNumber, int verseNumber, bool usedOnAPlayerCard,
    int mastery, int timePracticed, int timeUsedToHunt)
    {
    this.bibleReference = bibleReference;
    this.bookNumber = bookNumber;
    this.chapterNumber = chapterNumber;
    this.verseNumber = verseNumber;
    this.usedOnAPlayerCard = usedOnAPlayerCard;
    this.mastery = mastery;
    this.timePracticed = timePracticed;
    this.timeUsedToHunt = timeUsedToHunt;
    }
    }

    This is how I'm creating scriptable object

    [System.Serializable]
    [CreateAssetMenu(menuName = "Player Bible Data")]
    public class PlayerBible : ScriptableObject
    {
    public List<PlayerCard> playerCards = new List<PlayerCard>();
    public List<PlayerVerse> playerVerses = new List<PlayerVerse>();
    }

    Any help would be appreciative
     
  2. Livealot

    Livealot

    Joined:
    Sep 2, 2013
    Posts:
    228
    First, I wouldn't recommend using ScriptableObjects this way. Think of them as an intermediate step before serializing/deserializing to disk which is required for saved games.

    During development, there is a way to get this behavior by using EditorUtility.SetDirty(yourScriptableObject) which will save yourScriptableObject.assets file when you close the Editor. But SetDirty is not available in a build so that hack won't help for save games.
     
  3. roshaantariq

    roshaantariq

    Joined:
    Dec 6, 2017
    Posts:
    32
    Thank you for the reply. And yes I figured that out after unsuccessful attempts on platform device.