Inheritance stopped working in editor, only occurs in latest editor version

This problem only occurs in Unity 6 31f1, everything works fine in 2021.3.36f1.

I have an abstract ScriptableObject class “Item” and a derived class “MiscItem” that inherits from Item.

Whenever I try to assign a MiscItem object to an Item variable via editor (on a method, in button.onclick) I get this error:

Type cannot be found: MiscItem. Containing file and class name must match.

And if I try to assign the object to an Item List, the file picker does not recognize any MiscItem (or anything for that matters), though it does work only if i drag and drop the object to a List, or assign it to a Dictionary of Items via code through AssetDatabase.FindAssets with “*” as the filter string (couldn’t think of a filter which works).

These are the SO classes:

public abstract class Item : ScriptableObject
{
    public string ID;
    public string Description;
    public Sprite Icon;
    public int StackSize;
}
[CreateAssetMenu(fileName = "NewMiscItem", menuName = "Alex_M/InventorySys/Add Misc Item")]
public class MiscItem : Item
{
    
}

The method that caused me to seek help here:

public void TestAddItem(Item item)
{
    AddItem(item, 1);
}

I hope it’s not a bug and I got something wrong, but i have no clue what.

Hi, is your class MiscItem define in a file called MiscItem.cs?

Usually file containing ScriptableObject definition should:

  • Contain a single class definition deriving from ScriptableObject
  • The name of the file containing this definition should be the same as the class.

In your project it means you would need 2 files:

  • Item.cs containing class Item
  • MiscItem.cs containing class MiscItem

Thanks for the Answer!
Yes both classes are contained in a file with a matching name. But when I first created it the MiscItem class was just under the Item class in the same file, I immediately changed that after copying more assets from another project. Maybe something got “stuck”?

What puzzles me the most is that everything works under the hood, And I can even drag and drop the scriptable object to an exposed Item array in the editor, to then assign everything through code, yet in every other case the editor treats the derived class as if it was completely unrelated to the base class.

Hum this is indeed really strange.

1- To be sure that nothing got stuck: you can always delete the /Library folder. The whole project will reimport fresh. I know this is less than ideal. Especially if your project is big.

2- Alternatively, you can build a smaller project with your scripts: Item.cs and MiscItem.cs and see if your workflow would work. On my side I just did that and it seems the Object picker is showing me instance of MiscItem correctly (even thoug my property is of type Item):

I am using U6 btw.

1 Like

This can happen. Unity’s assetdatabase has corner case bugs where it misscharacterizes a type as being not valid for assignment even if it is. It’s very rare, and not consistently reproducable, but I have seen it happen every now and again.

The delete library thing should help, but it should really be enough to just reimport the script file for MiscItem and/or Item. If that doesn’t help, you might trick the asset database to do the correct thing by renaming MiscItem to something else (like ItemMisc), and then renaming back again after it manages to assign stuff.

1 Like

Deleting and recreating the scriptableobject solved the issue, also created another derived class and it worked too (couldn’t test properly yesterday, sorry for that).

I think that something about the scriptableobject I tested with didn’t refresh, or rebooting unity and the PC a few times fixed the class.

Thanks for the time.

I think @Baste is right on the money: somehow during the creation of your script, of instance of the scriptableobject, of the renaming of the script must have let some “corrupted bits” in AssetDatabase. Hence why the clear of the Library folder solved the issue. Granted this is not something that should happen… but at least it works for you now!

Thanks for the feedback.

Seb

1 Like