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

How do you create a new object instance from a Type that is derived from UnityEngine.Object.

Discussion in 'Scripting' started by MechaWolf99, Aug 18, 2019.

  1. MechaWolf99

    MechaWolf99

    Joined:
    Aug 22, 2017
    Posts:
    290
    As the title says, I want to create a instance of a Type that is derived from Object.


    Code (CSharp):
    1. public abstract class Base: Object
    2. { }
    3.  
    4. public class Child: Base
    5. { }
    6.  
    7. public class SomeClass : Editor
    8. {
    9.     Child _child;
    10.  
    11.     private void OnEnable ()
    12.     {
    13.           CreateInstanceOfType(_child.GetType());
    14.     }
    15.  
    16.     public void CreateInstanceOfType(Type type)
    17.     {
    18.         // This gives the error "type parameter is abstract and can't be used in the ObjectFactory".
    19.         Debug.Log(ObjectFactory.CreateInstance(type));
    20.         // So I tried this, but it simply returns null if Base derives from Object.
    21.         Debug.Log(Activator.CreateInstance(type));
    22.     }
    23. }
    I have been able to find very little online about it. And why Activator.CreateInstance would return null, in this case. Any thoughts would be greatly appreciated.
     
  2. MechaWolf99

    MechaWolf99

    Joined:
    Aug 22, 2017
    Posts:
    290
    So I did some more testing. But have still been unable to create an instance of a type that derives from Object. Using Activator.CreateInstance seems like the right thing though. Just still not sure why it wouldn't work with Object.
     
  3. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,051
    You're likely deriving from UnityEngine.Object, is that intentional?

    Unity's Object doesn't have a public constructor and you're not supposed to construct it. Activator is likely returning an instance but since it's not backed by a native object, it pretends to be null (like Unity Objects that have been destroyed).

    If you just want to create a basic class, it doesn't need to derive from object, that will happen implicitly. You can just write «public abstract class Base». If you need to reference .Net's Object when you have imported UnityEngine, write «System.Object» instead.
     
  4. MechaWolf99

    MechaWolf99

    Joined:
    Aug 22, 2017
    Posts:
    290
    Yeah I am deriving from UnityEngine.Object intentionally. The reason for deriving from it is that I want to use Editor.CreateEditor(_child); And it requires a UnityEngine.Object as a parameter. So Child needs to be, or derive from UnityEngine.Object for it to work.

    Oh, thank you for explaining that. That would make sense as to what it is doing.
     
  5. ABerlemont

    ABerlemont

    Joined:
    Sep 27, 2012
    Posts:
    66
    @MechaWolf99 did you end up solving this ? (or anyone ?)
    I'm trying to draw the "inspector" of a custom Type the same way.
     
  6. MechaWolf99

    MechaWolf99

    Joined:
    Aug 22, 2017
    Posts:
    290
    Oh wow, this brings me back haha.
    Yeah I can share some info for you and in case anyone else stumbles on this in the future.

    The first thing to note as Adrian said originally, is that you cannot inherit directly from
    UnityEngine.Object
    . It is a special class in Unity that needs to have a C++ counterpart, and other special things. Long story short, you can't inherit from it.

    With that out of the way, the question is how do you draw the 'inspector' for a normal C# class or struct? The answer is that you have to use the SerializedObject and SerializedProperty system. You can draw them using the EditorGUI.PropertyField, EditorGUILayout.PropertyField, or if you are using UIToolkit, PropertyField element.

    If you want to make a 'custom inspector' for a normal C# class like you would for a
    ScriptableObject
    or
    MonoBehaviour
    . Then you will want to inherit from PropertyDrawer instead of Editor. Which is basically
    Editor
    , but for normal C# classes and structs.

    The docs have a lot of examples of usage and such, and there is lots of info online about using them now. So I will not go in to too much detail on how to actually use them.

    But if you have more specific question about it or something, feel free to ask them! Hope this helps get you started!