1
\$\begingroup\$

I have a base class that has a lot of classes inheriting from it, I want to make a script that you can drop one of these derived classes into. How do i do that?

abstract class thingy : UnityEngine.ScriptableObject
{
    // methods, variables, etc
}

class awsomeThing : thingy
{
    // Imagine there are more of these classes with different names...
}

I've managed to get the thingy class to show up but (as you can guess) you can only drag in that script in there, is there a way to get the inspector to accept the multiple awsomeThing classes?

\$\endgroup\$
2
  • \$\begingroup\$ Polymorphic serialization in Unity is fraught. There are workarounds to get some aspects working the way you want, but generally speaking you'll have a smoother time if you can avoid serializing polymorphic references wherever possible. \$\endgroup\$
    – DMGregory
    Mar 27, 2016 at 23:16
  • \$\begingroup\$ Does turning on Debug mode in the inspector work? \$\endgroup\$
    – Evorlor
    Nov 3, 2019 at 14:58

1 Answer 1

2
\$\begingroup\$

I'm unable to reproduce the problem described in this question when using types that are derived from MonoBehaviour or ScriptableObject as shown above.

Using the following code:

public abstract class SomeBaseClass : ScriptableObject
{
    public int baseClassInt = 5;
}
    
[CreateAssetMenu(fileName ="Derived.asset", menuName ="Testing/Some Derived Class")]
public class SomeDerivedClass : SomeBaseClass
{
    public string derivedClassString = "foo";
}
    
public class PolymorphicTest : MonoBehaviour
{
    public SomeBaseClass someBaseInstance;
}

An instance of type SomeDerivedClass is accepted just fine in an inspector field of type SomeBaseClass:

enter image description here

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .