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 Scriptable Objects losing some values on play in editor

Discussion in 'Scripting' started by frothypants, Apr 20, 2021.

  1. frothypants

    frothypants

    Joined:
    Jul 23, 2016
    Posts:
    17
    Hey there, first time using scriptable objects and have randomly encountered an issue that I can't seem to nail down.

    There are no errors in the console and no script rewriting the values. Here is what is happening.
    1. Create instances of the scriptable object below in the editor
    2. Populate all fields
    3. Hit play in the editor
    4. Multiple scriptable objects lose their
      itemIcon
      and
      itemIconBackground
      sprite references
    5. Hit play again to stop in the editor
    6. Scriptable object variable valuable still missing
    I thought the benefit of scriptable objects was that the data would persist on these individual instances? Is there something I have set up incorrectly? Is this a bug I should submit?

    I've tried recreating the objects, changing names, etc with no luck. All scripts that interact with these only reference them, never attempt to modify them.

    Here is the scriptable object class. Using Unity 2020.3.3f1.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [CreateAssetMenu(fileName = "Ruby Terminal Item", menuName = "Ruby Terminal Item", order = 0)]
    4.  
    5. public class UmokTerminalItem : ScriptableObject
    6. {
    7.  
    8.     [SerializeField]
    9.     private bool itemActiveInStore = false;
    10.  
    11.     [SerializeField]
    12.     private string itemDisplayName = "Skin Name";
    13.  
    14.     [Space(30, order =4)]
    15.  
    16.     [SerializeField]
    17.     private string itemName = "skin_name";
    18.  
    19.     [Space(30, order = 8)]
    20.  
    21.     [SerializeField]
    22.     [TextArea(4,4)]
    23.     private string itemDescription = "";
    24.  
    25.     [Space(30, order =12)]
    26.  
    27.     [SerializeField]
    28.     private int UCCost = 50;
    29.  
    30.     [Space(30, order =14)]
    31.  
    32.     [Header("Icon that will display in the store")]
    33.     [SerializeField]
    34.     private Sprite itemIcon;
    35.     [SerializeField]
    36.     [Header("Background only used for weapons")]
    37.     private Sprite itemIconBackground;
    38.  
    39.     public enum terminalItemTypes
    40.     {
    41.         skin,
    42.         gun,
    43.         modifier,
    44.         weaponskin
    45.     };
    46.  
    47.     [Header("Terminal Item Type", order = 15)]
    48.     [SerializeField]
    49.     private terminalItemTypes terminalItemType = terminalItemTypes.skin;
    50.  
    51.  
    52.     [Space(30, order = 16)]
    53.     [Header("Weapon Skin Color. Only use if this is a weapon skin color", order = 17)]
    54.     [SerializeField]
    55.     private Color32 weaponSkinColor = new Color32(255, 255, 255, 255);
    56.     [SerializeField]
    57.     private Color32 weaponSkinColorSecondary = new Color32(255, 255, 255, 255);
    58.  
    59.  
    60.     [Space(30, order = 18)]
    61.  
    62.     [SerializeField]
    63.     private Color32 iconCoreColor = new Color32(255, 255, 255, 255);
    64.    
    65.     [SerializeField]
    66.     private Color32 iconBgColor= new Color32(255, 255, 255, 255);
    67.  
    68.     [SerializeField]
    69.     private UmokTerminalItem linkedWeaponSkin;
    70.  
    71.     public bool IsActive { get { return itemActiveInStore; } }
    72.     public string DisplayName { get { return itemDisplayName; } }
    73.     public string ItemID { get { return itemName; } }
    74.     public string Description { get { return itemDescription; } }
    75.     public int Cost { get { return UCCost; } }
    76.     public Sprite ItemIcon { get { return itemIcon; } }
    77.     public Sprite ItemIconBackground { get { return itemIconBackground; } }
    78.     public terminalItemTypes Type { get { return terminalItemType; } }
    79.     public Color32 WeaponSkinColor { get { return weaponSkinColor; } }
    80.     public Color32 WeaponSkinColorSecondary { get { return weaponSkinColorSecondary; } }
    81.     public Color32 IconCoreColor { get { return iconCoreColor; } }
    82.     public Color32 IconBackgroundColor { get { return iconBgColor; } }
    83.     public UmokTerminalItem LinkedWeaponSkin { get { return linkedWeaponSkin; } }
    84. }
    85.  
     
  2. TheNightglow

    TheNightglow

    Joined:
    Oct 1, 2018
    Posts:
    201
    there was something you need to do to make scriptable objects actually save their info....

    something like
    EditorUtility.SetDirty(<yourObject>);
    to mark that there were changes on it that need to be saved..... and...

    i think one of the AssetDataBase functions afterwards if that does not suffice

    something like that should be the solution^^
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
  4. frothypants

    frothypants

    Joined:
    Jul 23, 2016
    Posts:
    17
    Thanks, guys. I thought these were only the case when editing scriptable objects outside of the (default) unity editor/inspector window? Is that not the case?
     
  5. TheNightglow

    TheNightglow

    Joined:
    Oct 1, 2018
    Posts:
    201
    nah, well the reason is that the assets you are using to create your game shouldnt be possible to accidently be altered...

    if you could change the scriptable object without an explicit function that says that it should be saved, you could easily accidently mess with it every time you click play....

    like imagine you use scriptable objects to define items the player can pick up, and allow the player to rename items in game, you might very well accidently write code that will overwrite your default item names whenever you click play in unity just to test your game