- Home /
The question is answered, right answer was accepted
Awarding 400 points if we solve this ---- AssetDatabase.LoadAsset("path") loads (null) instead of scriptable object (after enter/exit playmode)
Friends, I noticed a very strange behaviour when working with scriptable objects.
When creating a ScriptableObject in any folder (in this example - 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.
using UnityEngine;
using System.Collections;
public class mySO : ScriptableObject {
int i;
void OnEnable(){
i = 1;
}
}
and the window to create and load the object from the paths.
using UnityEngine;
using System.Collections;
using UnityEditor;
public class CreateSOwin : EditorWindow {
[MenuItem("Window/create SO")]
public static void CreateWindow(){ //create a window through menu
CreateSOwin window = (CreateSOwin) EditorWindow.GetWindow(typeof(CreateSOwin));
window.Show ();
}
void OnGUI(){ //button to create a scriptable object
if(GUILayout.Button("create a so in Resources")){
if(EditorApplication.isPlaying != true){
mySO so = ScriptableObject.CreateInstance<mySO>();
AssetDatabase.CreateAsset(so, "Assets/Resources/mySO.asset"); //save it for later use
EditorUtility.SetDirty(so);
AssetDatabase.SaveAssets();
Debug.Log (" we are in editor.creating so at Assets/Resources/mySO.asset");
}
}
GUILayout.Space (20);
//button to load the scriptable object from path (during the playmode):
if(GUILayout.Button("load SO from resources (during gameplay)")){
if(Application.isPlaying || EditorApplication.isPlaying )
Debug.Log (" we are playing. What's sitting at Resources.Load('mySO.asset')? ----> " + Resources.Load("mySO"));
}
//button to load the scriptable object during the editor
if(GUILayout.Button("load SO from asset/resources (during editor)")){
if(EditorApplication.isPlaying != true )
Debug.Log (" we are in editor. What's sitting at AssetDatabase.LoadAssetAtPath<mySO>('Assets/Resources/mySO.asset')? ----> "
+ ( AssetDatabase.LoadMainAssetAtPath("Assets/Resources/mySO.asset") as mySO));
}
}//end OnGUI()
}
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.
Answer by IgorAherne · Aug 28, 2015 at 01:04 AM
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!! )
Follow this Question
Related Questions
Prefab as a subasset of a ScriptableObject 0 Answers
AssetDatabase.CreateAsset() - Value cannot be null 2 Answers
Custom assets give Missing (Mono Script) 0 Answers
How to change an object's name inside an asset file? 0 Answers
Unity loses association to ScriptableObject after exiting playmode 0 Answers