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
  4. Dismiss Notice

Unity 4.3 ScriptableObject not loaded on startup (C#)

Discussion in 'Editor & General Support' started by ZoltanErdokovy, Nov 26, 2013.

  1. ZoltanErdokovy

    ZoltanErdokovy

    Joined:
    Aug 23, 2012
    Posts:
    100
    When I start the editor one of the classes, a ScriptableObject, is not recognized. Loading
    fails silently, there is nothing in the log, I just can't use it from scripts. The instances of it
    are still present in custom asset containers but selecting them shows that label in the
    inspector: "Couldn't load script, fix compile errors."

    I have many similar classes which are working fine, the only special thing about this class
    is that I finished developing it after the 4.3 update.

    If right after startup I put a space somewhere in the .cs file and force a recompile that way
    then things work once again.
     
  2. ZoltanErdokovy

    ZoltanErdokovy

    Joined:
    Aug 23, 2012
    Posts:
    100
    The issue is still present in 4.3.1.
     
  3. ZoltanErdokovy

    ZoltanErdokovy

    Joined:
    Aug 23, 2012
    Posts:
    100
    During build for web player the following warnings show up in the log:

    And indeed all instances become null. Happens even if things are working in the editor.
     
  4. ZoltanErdokovy

    ZoltanErdokovy

    Joined:
    Aug 23, 2012
    Posts:
    100
    I narrowed down the problem to this line:

    Code (csharp):
    1. UnityEngine.Object[] subObjects = AssetDatabase.LoadAllAssetsAtPath(theAssetFilePath);
    It does not load the instance of that particular class so it becomes null in the Object array.
    At least until I force a recompile. The issue seems to be limited to asset management as
    I can always create new instances in the scene.
     
  5. ZoltanErdokovy

    ZoltanErdokovy

    Joined:
    Aug 23, 2012
    Posts:
    100
    If I browse the custom asset container in Unity's Hierarchy view then I can see all SO instances
    except the ones which are having the issue. The weird thing is that I can manually pick the
    class it is complaining about:
     

    Attached Files:

    Last edited: Dec 4, 2013
  6. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Did you move the class into a dll or maybe you took it out of a dll?
     
  7. ZoltanErdokovy

    ZoltanErdokovy

    Joined:
    Aug 23, 2012
    Posts:
    100
    Hmm, interesting that you mention it. For a while I was using the dll version of this plugin but
    debugging was difficult so I moved onto using the source version long before updating to 4.3.
    I double checked and there is nothing else in there anymore for DLL building, I fully removed
    that feature.
     
  8. ZoltanErdokovy

    ZoltanErdokovy

    Joined:
    Aug 23, 2012
    Posts:
    100
    Okay, here is what's happening: if the class was added from Visual Studio then the problem
    occurs, however if the class was created in Unity then it's fine. o_O
     
  9. ZoltanErdokovy

    ZoltanErdokovy

    Joined:
    Aug 23, 2012
    Posts:
    100
    One more interesting thing: Older custom assets contain the following lines where they
    define the now broken instances:
    These instances become null on load.
    This can be fixed like this:
    I have no idea what this means but might help to track down the underlying bug.
     
  10. Flavelius

    Flavelius

    Joined:
    Jul 8, 2012
    Posts:
    926
    i have the exact same problem with a class created by myself inside Unity. At every startup the created assets lose their reference to the ScriptableObject class they represent. i dont have namespaces nor do i use dll's.
    And it's fixed if i manually re-assign it from the inspector.

    Edit: i think i found the solution for my specific problem - The ScriptableObject derived class needed to be in its own .cs file. If i appended it to another file it would do the above. But now it works.
     
    Last edited: Mar 25, 2014
  11. Crouching-Tuna

    Crouching-Tuna

    Joined:
    Apr 4, 2014
    Posts:
    81
    Bumping thread.
    I'm having the same problem. I tried putting every ScriptableObject derived class to its own .cs script, and it works but only in editor, but... all my SO always have missing reference on startup. Pressing play, however, fixes it and throughout that Unity session, it'll always remember the reference.

    The problem comes when I try it on build. It seems like that "missing reference" on startup also happens on the build, but this time it doesn't "fix" it.

    I can't seem to find the solution anywhere...
     
  12. shallwaycn

    shallwaycn

    Joined:
    Apr 3, 2012
    Posts:
    202
    Maybe you should not put the ScriptableObject class into "Editor" folder.
     
  13. Roiw

    Roiw

    Joined:
    Jan 15, 2013
    Posts:
    23
    I have the same problem. Have no idea how to fix this.. Have someone found a fix?