Search Unity

  1. Check out our Unite Austin 2017 YouTube playlist to catch up on what you missed. More videos coming soon.
    Dismiss Notice
  2. Unity 2017.2 is now released.
    Dismiss Notice
  3. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  4. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  6. Unity 2017.3 beta is now available for download.
    Dismiss Notice

ScriptableObject's cannot be placed within namespaces unless in DLLs?

Discussion in 'Editor & General Support' started by numberkruncher, Sep 4, 2013.

  1. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    938
    This issue still occurs with the latest version of Unity 4.2.1. Whilst I may be mistaken, I was under the impression that Unity 4.0 was suppose to support namespaces within scripts now.

    When this issue does occur, attempting to delete the broken asset file causes Unity to crash to the desktop with some MemoryStream exception.

    Oddly, it is (and has for quite a while now) possible to place ScriptableObject's into custom namespaces if you compile them into DLL's. But for some reason attempting to do this with separate script assets does not work.

    I have reported this using the bug reporter. Here is the case number should anyone be interested:
    Case 561547
     
  2. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,321
    Can't be certain on ScriptableObjects, but namespaces still aren't fully supported for some objects. You can't put a Monobehavior in a namespace whether its in a script or a DLL, it just doesn't work. Your best bet is to not use namespaces for ScriptableObjects either.
     
  3. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,669
    @numberkruncher, I have not problem to use ScriptableObject in namespaces. In my Decal System Pro, I have a TextureAtlasAsset which is a ScriptableObject within a namespace. In my development environment, it is obviously not in a dll, while in the final version the class is part of a dll. I had not problems with it.

    @Dustin Horne, lots of my classes are in namespaces, including scripts derived from MonoBehaviour, Editor, ... . This works since Unity 4 and I never had issues with it so far. What is not working for you?
     
  4. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,321
    Interesting, I'll have to give it a try again. It's been awhile since I tried it but with 4.0 I was still getting errors when putting a MonoBehavior derivative in a namespace but I may have missed something. My MonoBehavior in that instance was in a compiled assembly.
     
  5. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,669
    It works in dlls too for me.
     
  6. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    938
    MonoBehaviour classes seem to work fine within namespaces for me (both in and out of DLL's):

    "Scripting: MonoBehaviours can now be inside namespaces."
    - http://unity3d.com/unity/whats-new/unity-4.0

    Okay, I have narrowed the source of the problem down and it is 100% reproducible. This problem is caused when a custom scriptable object class is placed within a namespace which contains a function with a default parameter.

    The following editor script is used in each of the following test cases:
    Code (csharp):
    1.  
    2. // Assets/Editor/Test.cs
    3. using UnityEngine;
    4. using UnityEditor;
    5. using My.Namespace; // Cases 3, 4 and 5
    6.  
    7. public static class Test {
    8.     [MenuItem("Custom/Foo")]
    9.     static void Foo() {
    10.         var ms = ScriptableObject.CreateInstance<MyScriptable>();
    11.         AssetDatabase.CreateAsset(ms, "Assets/test.asset");
    12.     }
    13. }
    14.  
    Case 1: Works Fine - Bare Bones
    Code (csharp):
    1.  
    2. // Assets/Scripts/MyScriptable.cs
    3. using UnityEngine;
    4.  
    5. public class MyScriptable : ScriptableObject {
    6.     public int someNumber;
    7.     public void Foo(int xyz) {
    8.         someNumber = xyz;
    9.     }
    10. }
    11.  
    1. Select menu Custom|Foo.
    2. Select asset in project view and inspect.
    3. All good.
    4. Delete asset.

    Case 2: Works Fine - Namespace
    Code (csharp):
    1.  
    2. // Assets/Scripts/MyScriptable.cs
    3. using UnityEngine;
    4.  
    5. namespace My.Namespace {
    6.     public class MyScriptable : ScriptableObject {
    7.         public int someNumber;
    8.         public void Foo(int xyz) {
    9.             someNumber = xyz;
    10.         }
    11.     }
    12. }
    13.  
    1. Select menu Custom|Foo.
    2. Select asset in project view and inspect.
    3. All good.
    4. Delete asset.

    Case 3: Works Fine - Default Function Param
    Code (csharp):
    1.  
    2. // Assets/Scripts/MyScriptable.cs
    3. using UnityEngine;
    4.  
    5. public class MyScriptable : ScriptableObject {
    6.     public int someNumber;
    7.     public void Foo(int xyz = 123) {
    8.         someNumber = xyz;
    9.     }
    10. }
    11.  
    1. Select menu Custom|Foo.
    2. Select asset in project view and inspect.
    3. All good.
    4. Delete asset.

    Case 4: Fails - Default Function Param + Namespace
    Code (csharp):
    1.  
    2. // Assets/Scripts/MyScriptable.cs
    3. using UnityEngine;
    4.  
    5. namespace My.Namespace {
    6.     public class MyScriptable : ScriptableObject {
    7.         public int someNumber;
    8.         public void Foo(int xyz = 123) {
    9.             someNumber = xyz;
    10.         }
    11.     }
    12. }
    13.  
    1. Select menu Custom|Foo.
    2. Select asset in project view and inspect.
    3. Inspector indicates that script is missing sometimes saying "None (Mono Script)" otherwise oddly "None (Mono Behaviour)".
    4. Delete asset.
    5. CRASH

    Case 5: Works Fine - Same as Case 4 but inside a DLL
     
  7. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,669
    You are right. I was able to reproduce that as well. I never encounter the issue because I don't use default values in my Decal System.