Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Jason-King · Apr 16, 2014 at 07:55 PM · unityeditorassetdatabase

CreateAsset assets failing to load on Editor restart

I have created a test project where all it initially contained was an Assets/Plugin/Build/TestSettings.cs file...

 using UnityEngine;
 using UnityEditor;
 using System;
 using System.IO;
 
 namespace OneLoop
 {
     [InitializeOnLoad]
     public class Test : ScriptableObject
     {
         [SerializeField]
         public string bar;
         
         protected const string BUILDSETTINGS_FOLDER = "Assets/Resources/";
         protected const string BUILDSETTINGS_NAME = "TestSettings";
         
         protected static Test instance = null;
         
         public static Test inst
         {
             get
             {
                 if (instance == null)
                 {
                     instance = Resources.Load(BUILDSETTINGS_NAME) as Test;
                     
                     if (instance == null)
                     {
                         string path = GetProjectFolder() + "/" + BUILDSETTINGS_FOLDER;
                         
                         if (!Directory.Exists(path))
                         {
                             Directory.CreateDirectory(path);
                         }
                         
                         instance = CreateInstance<Test>();
                         
                         if (instance != null)
                         {
                             Debug.LogWarning("Creating an instance of type " + "Test" + " in file " + BUILDSETTINGS_FOLDER + BUILDSETTINGS_NAME + ".asset");
                             AssetDatabase.CreateAsset(instance, BUILDSETTINGS_FOLDER + BUILDSETTINGS_NAME + ".asset");
                             EditorUtility.SetDirty(instance); 
                             AssetDatabase.SaveAssets();
                             Debug.LogWarning(AssetDatabase.GetAssetPath(instance));
                         }
                         else
                         {
                             Debug.LogWarning("Failed to create instance of type " + "Test");
                         }
                     }
                 }
                 
                 return instance;
             }
         }
         
         public static string GetProjectFolder()
         {
             return Application.dataPath.Replace("/Assets", "");
         }
         
         static Test()
         {
             Debug.LogWarning(inst.ToString());
         }
     }
 }

Upon Editor startup, its InitializeOnLoad call attempts to check to see if the "TestSettings.asset" resource already exists and if it fails calls CreateAsset to create it. This all works fine and while my Editor is running I can modify/rebuild the code (which reinitializes all classes) and see that Resources.Load now properly loads my asset. However, as soon as I shutdown and restart the Editor, Resources.Load will fail to find my TestSettings.asset.

I'm using some middleware that attempts to do the same thing that I am and their classes are working. I've looked at the difference between their asset files and mine and I see difference. In their files, their m_Script contains data (such as m_Script: {fileID: 11500000, guid: 461bfb74be57ce74eac44498472f0ad0, type: 3}) while mine is blank. Additionally, my m_EditorClassIdentifier contains data (such as m_EditorClassIdentifier: Assembly-CSharp:OneLoop:Test) while theirs is blank.

I've tried Resources.Load and .LoadAssetAtPath as well as equivalent AssetDatabase load object calls and they are all failing for me. What am I doing wrong here?

Please advise.

Thanks, Jason

test.zip (101.3 kB)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Jason-King · Apr 16, 2014 at 09:29 PM

Apparently Unity requires that my class and its filename need to be the same for a proper script connection to work. I was using class Test with filename TestSettings.cs. If I rename TestSettings.cs to Test.cs then it all started to work.

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 Jason-King · May 15, 2014 at 12:03 AM 0
Share

Additionally placing the class within a namespace can result in the same issue. Not sure why - because I have some classes that work in a namespace and now one that doesn't. Additionally I've seen both namespace and non-namespace versions in other middleware examples.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

Override asset on import 0 Answers

Unity recompilation time slowly increases each time it's recompiled. Why? 0 Answers

How To Store Meshes in Game [Not in Editor mode]? 1 Answer

Unity white screen partial 1 Answer

Plug and Play UnityEvents/Methods? 1 Answer


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