"UnityObject myObj = new Unity.Object()" always result in null

Hi,

I need to create an instance of a class inheriting from Unity.Object.
I am aware of the warnings about direct object creation but I can’t manage to get a non-null instance.
The following code always results in a null value for myObj :

Object myObj = new Object();

I also try to use the Instantiate methods, but I can’t go further because I don’t have (and I don’t want to) an original already instantiated Object.

Is there a way to achieve direct object creation ?

Thanks

Why do you want to do this? A base Unity.Object is completely useless.

I am playing with SerializedObject/Property and I need an Object for the init process.

I guess Unity’s Object class is not meant to be created manually or derived from. Only alternative I could think of is ScriptableObject, and still you can’t create it with new keyword, you have to do it with ScriptableObject.CreateInstance. Or you can simply create a GameObject with “new GameObject()” to play with.

It always annoys me when someone replies to a thread by asking “why do you want to know this?” Maybe they want to truly understand the engine that they are using, and having such strange behavior with fairly poor documentation makes that difficult for them. If I try to new up a UnityEngine.Object and it gives me a null I am sure going to be curious about what is going on.

If you use something like ILSpy you will see that Unity has overridden such operators as == on UnityEngine.Object and they rely on a CompareBaseObjects() to get the result. I do not know what this function does (outside the obvious) but it is probably responsible for you thinking that you have a null Object.

@pwc, most people who post are trying to achieve something specific, in order to help them you need to know what they are trying to achieve. It can take quite a bit of work to provide a good answer to some questions, and it can be a bit frustrating if you provide some in depth answer only to a response back like “oh I just wanted to show a box on the screen”.

Don’t think of why as a challenge; it’s just a question to which a valid answer is “I want to know how Unity works”.