Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Aug 28, 2015 at 01:48 AM by SarperS for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by IgorAherne · Aug 27, 2015 at 09:13 PM · assetscriptableobjectnullassetdatabaseplaymode

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image IgorAherne · Aug 28, 2015 at 12:29 AM 0
Share

.....bump :)

1 Reply

  • Sort: 
avatar image
0
Best Answer

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!! )

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Gadyel · Aug 28, 2015 at 02:09 AM 0
Share

Wew, it got me all spiced up.

Follow this Question

Answers Answers and Comments

25 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges