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 ScriptableObject asset gets reset by other team member (2019.4.2f1)

Discussion in 'Scripting' started by melos_han_tani, Nov 11, 2021.

  1. melos_han_tani

    melos_han_tani

    Joined:
    Jan 11, 2018
    Posts:
    77
    Hi, I am storing some data in a single asset based on this ScriptableObject class:

    upload_2021-11-11_10-11-54.png


    This is the asset:

    upload_2021-11-11_10-12-42.png

    PROBLEM: Whenever the other team member gets this file from version control (while unity is open), the file gets reset to its default values.

    Here is the code I use to load the asset into an editorwindow.

    upload_2021-11-11_10-15-22.png

    (The commented out part was when I had code that would create the asset, but I only need one of the asset so I commented it out.)


    ---

    I'm not sure if it's relevant, but in some other functions, 'data' is accessed.

    OnGUI:

    Code (CSharp):
    1.         active_palette_index = EditorGUILayout.Popup("Palette", active_palette_index, data.palette_names);
    2.  

    Update:
    Code (CSharp):
    1.  
    2.  
    3.        if (!data) {
    4.             return;
    5.         }
    6.      
    7.      
    8. // Elsewhere in the Update() look:
    9.  
    10.             SerializedObject data_so = new SerializedObject(data);
    11.             data_so.Update();
    12.             active_palette_sp_array = data_so.FindProperty(data.palette_names[active_palette_index] + "_prefabs");
    13.  
    14. ---
    15.  
    16.  
    17.  

    Anyways, what am I doing wrong? Is there a better or proper place to load the palette data? Thanks.
     
  2. melos_han_tani

    melos_han_tani

    Joined:
    Jan 11, 2018
    Posts:
    77
    One thing I realized is that I don't actually need to turn it into a SerializedObject as I'm never modifying the ScriptableObject asset from my scripts. Could that be causing a problem?
     
  3. melos_han_tani

    melos_han_tani

    Joined:
    Jan 11, 2018
    Posts:
    77
    Well, I seem to have fixed it. (We'll see, though... ) Removing "AssetDatabase.Refresh()" did the trick. I have zero idea why this is the case, but who cares!

    Some things I learned in the meantime:

    My SerializedObject code would have no effect, as it only reads the properties. I would need to call "ApplyModifiedProperties" on the SerObj to have modified the on-disk ScrObj.

    Fields in the ScrObj class being private - or any custom data types - not marked as serializable, would cause problems. But I wasn't doing that.

    Likewise, if I were to modify the ScrObj in my custom EditorWindow, then I'd need to call "mark dirty" or something. But that's a bad idea since whole SerObj API is meant to handle that stuff for you.