Search Unity

AssetDatabase.LoadAsset("path") loads (null) instead of scriptable object (after enter/exit playmode

Discussion in 'Asset Database' started by IgorAherne, Aug 27, 2015.

  1. IgorAherne

    IgorAherne

    Joined:
    May 15, 2013
    Posts:
    393
    I noticed a very strange behaviour when working with scriptable objects. I strongly suspect it has t do with the serialization :D

    When creating a ScriptableObject in Resources folder, after we enter /exit play mode, any attempts to load an asset from that path (in the editor) result in null being returned.

    However, we can still access such asset without any problems even if we change something in the script (add extra space) and hit save, coming back to unity. But as soon as we enter / exit playmode, the problem will pop out.


    Code (CSharp):
    1.   using UnityEngine;
    2.     using System.Collections;
    3.  
    4.     public class mySO : ScriptableObject {
    5.         int i;
    6.  
    7.         void OnEnable(){
    8.             i = 1;
    9.         }
    10.     }
    and the window to create and load the object from the paths.
    Code (CSharp):
    1.  
    2.     using UnityEngine;
    3.     using System.Collections;
    4.     using UnityEditor;
    5.  
    6.     public class CreateSOwin : EditorWindow {
    7.  
    8.         [MenuItem("Window/create SO")]
    9.         public static void CreateWindow(){ //create a window through menu
    10.             CreateSOwin  window = (CreateSOwin) EditorWindow.GetWindow(typeof(CreateSOwin));
    11.             window.Show ();
    12.         }
    13.  
    14.  
    15.  
    16.         void OnGUI(){    //button to create a scriptable object
    17.             if(GUILayout.Button("create a so in Resources")){
    18.                 if(EditorApplication.isPlaying != true){
    19.                     mySO so = ScriptableObject.CreateInstance<mySO>();
    20.                     AssetDatabase.CreateAsset(so, "Assets/Resources/mySO.asset"); //save it for later use
    21.  
    22.                     EditorUtility.SetDirty(so);
    23.                     AssetDatabase.SaveAssets();
    24.  
    25.                     Debug.Log (" we are in editor.creating so at Assets/Resources/mySO.asset");
    26.                 }
    27.             }
    28.             GUILayout.Space (20);
    29.  
    30.  
    31.             //button to load the scriptable object from path (during the playmode):  
    32.             if(GUILayout.Button("load SO from resources (during gameplay)")){
    33.                 if(Application.isPlaying ||  EditorApplication.isPlaying )
    34.                     Debug.Log (" we are playing. What's sitting at Resources.Load('mySO.asset')? ---->  " + Resources.Load("mySO"));
    35.             }
    36.          
    37.  
    38.       //button to load the scriptable object during the editor
    39.             if(GUILayout.Button("load SO from asset/resources (during editor)")){
    40.                 if(EditorApplication.isPlaying != true )
    41.                     Debug.Log (" we are in editor. What's sitting at AssetDatabase.LoadAssetAtPath<mySO>('Assets/Resources/mySO.asset')?  ---->  "
    42.                               + ( AssetDatabase.LoadMainAssetAtPath("Assets/Resources/mySO.asset") as mySO));
    43.             }
    44.         }//end OnGUI()
    45.     }

    to see the problem I do the following:

    .

    1) open the window

    2) create a scriptable object.

    3) check that its nicely loaded from the path, by clicking "load SO from asset/resources (during editor)". **See in the console that it's all good** :D

    4) jump into the script, add a space, save and go back to Unity. It re-loads everything (See if the loading sign in the bottom right corner has disappeared).

    5) check the scriptable object, by again clicking "load SO from asset/resources (during editor)". The console sais that there **is indeed a reference to such object.** This happens even after the scripts were re-compiled!

    Now enter the playmode, hit "load SO from resources (during gameplay"). **The reference is still there!**

    6) exit play mode and click check SO from assets/resources (during editor") **- the reference in the console is returned as null**

    Could somebody help me out?

    .

    PS - I am placing the asset in Resources folder, since the asset has to be loaded at runtime, and initially is not referenced directly from anywhere in the scene (based on this post)

    Also, there has to be a folder called Resources for the asset to be created correctly.
     
  2. IgorAherne

    IgorAherne

    Joined:
    May 15, 2013
    Posts:
    393
    Turns out my Scriptable object had to be in a separate script."

    Also, script file containing the scriptable object has to have the exact same name as the scriptable object's class name.

    ...Duh!! )
     
    plujak and AndyGainey like this.