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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Serialization Error with scriptable objects.

Discussion in 'Scripting' started by anueves1, Nov 14, 2015.

  1. anueves1

    anueves1

    Joined:
    Jan 10, 2014
    Posts:
    36
    Hi,
    I've been working on a cool camera system and i need to make some scriptable objects to hold different types of cameras(The end user can make it's own types and save them) and i've run into an error that happens when i save a scriptable object and then restart the editor, the object just tells me that the associated script cannot be loaded.

    Here is my code:

    Camera Editor(Saves and loads scriptable objects):

    Code (CSharp):
    1.         if (GUILayout.Button("Save Custom CameraType"))
    2.         {
    3.             //Save as scriptable object so it can be edited and then load the types in here.
    4.             CameraVariablesEditor.CreateCameraType(myTarget);
    5.         }
    6.  
    7.         if(GUILayout.Button("Load Custom CameraType"))
    8.         {
    9.             CameraVariablesEditor.LoadCameraType(myTarget);
    10.         }
    11.  
    That bit saves and loads SOs from this class:

    Code (CSharp):
    1. [CustomEditor(typeof(CameraVariables))]
    2. public class CameraVariablesEditor : Editor
    3. {
    4.   public override void OnInspectorGUI()
    5.   {
    6.   CameraVariables myTarget = target as CameraVariables;
    7.  
    8.   //Some variable assignment goes here.
    9.  
    10.   if(GUI.changed)
    11.   EditorUtility.SetDirty(target);
    12.  
    13.   }
    14.  
    15.   public static void CreateCameraType(CameraMovement target)
    16.   {
    17.   string Path = EditorUtility.SaveFilePanel("Create Camera Type", "Assets/Data Containers/Camera Data", "default.asset", "asset");
    18.  
    19.   Path = FileUtil.GetProjectRelativePath(Path);
    20.  
    21.   CameraVariables ctc = CreateInstance<CameraVariables>();
    22.   AssetDatabase.CreateAsset(ctc, Path);
    23.  
    24.   CameraMovementEditor.GiveDefaultValuesToStuffOnCameraVariables(target, ctc);
    25.  
    26.   AssetDatabase.SaveAssets();
    27.   }
    28.  
    29.   public static void LoadCameraType(CameraMovement target)
    30.   {
    31.   string Path = EditorUtility.OpenFilePanel("Create Camera Type", "Assets/Data Containers/Camera Data", "asset");
    32.  
    33.   if (Path == string.Empty)
    34.   return;
    35.  
    36.   Path = FileUtil.GetProjectRelativePath(Path);
    37.  
    38.   CameraVariables ctc = AssetDatabase.LoadAssetAtPath<CameraVariables>(Path);
    39.   CameraMovementEditor.GiveDefaultValuesToStuffOnCameraMovement(ctc, target);
    40.   }
    41. }
    And the scriptable object class is this one:

    Code (CSharp):
    1. [System.Serializable]
    2. public class CameraVariables : ScriptableObject
    3. {
    4.     public bool CanCameraMovePosition;
    5.     public float CameraSpeed = 0.04f;
    6.  
    7.     public UpdateType m_UpdateType = UpdateType.Update;
    8.  
    9.     public bool CanCameraSprint = false;
    10.     public float CameraSpeedShifted = 0.08f;
    11.  
    12.     public bool UseRightClickMouseLook = false;
    13.     public bool SmoothCamera = false;
    14.     public float CameraSmoothing = 20.0f;
    15.  
    16.     public float SensivityX = 2.0f;
    17.     public float SensivityY = 2.0f;
    18.     public KeyCode MouseLookKey = KeyCode.Mouse1;
    19.  
    20.     public bool MouseZoom = false;
    21.     public float WheelStep = 1f;
    22.  
    23.     public bool AltZoom = false;
    24.     public float ZoomSmoothing = 20.0f;
    25.     public KeyCode AlternateZoomKey = KeyCode.LeftAlt;
    26.  
    27.     public bool CanSelectObjects = false;
    28.     public Color SelectedColor = Color.white;
    29. }
    30.  
    Thanks.
     
  2. anueves1

    anueves1

    Joined:
    Jan 10, 2014
    Posts:
    36
  3. anueves1

    anueves1

    Joined:
    Jan 10, 2014
    Posts:
    36
    Bump x2
     
  4. anueves1

    anueves1

    Joined:
    Jan 10, 2014
    Posts:
    36
    Bump x3
     
  5. Lex-DRL

    Lex-DRL

    Joined:
    Oct 10, 2011
    Posts:
    139
    I had the same issue today.
    I was able to create a new ScriptableObject and save it to asset, and the resulting asset was just fine.
    But after Unity restart i've got the same error.

    I tried modifuing my script here and there and restarting Unity for a few dozens of times, until I noticed... that the asset is initially created with the broken link. I.e., the "Script" field in the Inspector tells "None (MonoBehavior)" or something like that immediately after asset creation.

    As it turns out, Unity can't find my ScriptableObject class if it's defined anywhere but inside it's own file. So,

    [SOLUTION]
    If your ScriptableObject is defined in the same file as the main class (that's supposed to use it's data), just move it to it's own file (i.e., define it the same way you do for MonoBehaviors).