Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Weird ScriptableObject Error

Discussion in 'Scripting' started by AEG89, Dec 31, 2018.

  1. AEG89

    AEG89

    Joined:
    Apr 14, 2018
    Posts:
    2
    Hi! I was making an inGame Console to execute some methods while playing and everythig was fine, but now it's broken and I really need help to fix the problem couse i really don't understand the problem. Let me explain you:

    Console is a canvas with Text attached. I'm using a Command Pattern to execute the methods wich derive from an abstract base class(CNS_Commands) and the Base Class is an ScriptableObject. (i.e. CNS_Commands: ScriptableObject --> CloneCMD : CNS_Commands)
    then I use the CreateAssetMenu thing to make it work and everything was fine, but then I deleted the ScriptableObjectAsset by Right-clicking on it and just deleting it, and now I cannot create another Asset... it throws this on console:

    "Instance of CNS_Commands couldn't be created. The the script class needs to derive from ScriptableObject"
    "NullReferenceException: Object reference not set to an instance of an object
    UnityEditor.ProjectWindowUtil.CreateAsset (UnityEngine.Object asset, System.String pathName) (at /Users/builduser/buildslave/unity/build/Editor/Mono/ProjectWindow/ProjectWindowUtil.cs:187)"

    But the weird thing is... CNS_Commands DOES DERIVE from ScriptableObject...so I really need help to fix it couse I dont understand the problem... I reinstalled Unity (v5.6.5) and also tried to create another Script copying the older, but nothing worked . Thank you in advance people!
     
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,589
    Unity requires to have classes that are derived from ScriptableObject and MonoBehaviour to be located in a file with the same name. Otherwise it can't serialize the data.

    Is the class CNS_Commands located in a file named CNS_Commands.cs?
     
    ow3n likes this.
  3. AEG89

    AEG89

    Joined:
    Apr 14, 2018
    Posts:
    2
    Well, yes, everything was correctly named and located ( I though, I got frenetic and erased, changed lot of things), so finally I opened up another project, I made a new Console and CNS_Commands script and worked... then I saw your replay and erased the old scripts In the original project and created new ones copied from the new-and-working project... and everything is fine again!
    so, thank you man, you are a hero ( seems like it was easy to fix, but live is hard for a newbie hehe)
     
  4. Nicoschief

    Nicoschief

    Joined:
    Jul 27, 2022
    Posts:
    1
    I have the same issue and am also using a abstract class that derives from ScriptableObject. Could it be that Unity support abstract ScriptableObjects?
     
  5. kmaxi

    kmaxi

    Joined:
    Nov 14, 2022
    Posts:
    2
    Also getting this error. Any solutions?
     
  6. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,297
    It's not possible to create instances of an abstract class. That is basically the whole purpose of the abstract modifier: it indicates that this class is only meant to be used as a base class and not on its own.

    Abstract classes can even define members without any implementation:
    Code (CSharp):
    1. public abstract class ItemBase : ScriptableObject
    2. {
    3.     public abstract string Name { get; } // no implementation here
    4.     public abstract void Use(Actor target); // no implementation here
    5. }
    It is not possible to create an instance of an abstract class, because they are still incomplete.
    Code (CSharp):
    1. ItemBase itemBase = ScriptableObject.CreateInstance<ItemBase>(); // not possible
    2. itemBase.Use(target); // this method doesn't even have an implementation
    To create an actual usable, concrete class from an abstract one, you need to derive from it and give all its abstract members an implementation.
    Code (CSharp):
    1. public class Sword : ItemBase
    2. {
    3.     public override string Name => "Sword";
    4.     public override void Use(Actor target) => target.Damage(10);
    5. }
    Code (CSharp):
    1. Sword sword = ScriptableObject.CreateInstance<Sword>(); // this works
    2. sword.Use(target); // this actually makes sense