Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

New UnityEngine.Object is Null

Discussion in 'Editor & General Support' started by DireLemming, Jul 26, 2011.

  1. DireLemming

    DireLemming

    Joined:
    Jul 8, 2011
    Posts:
    7
    Can anyone explain why new derived instances of UnityEngine.Object are immediately null after being instantiated in this use case? I'm on 3.3.

    Code (csharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using UnityEngine;
    5.  
    6. public class MyObject : UnityEngine.Object
    7. {
    8.     public string MyField;
    9. }
    10.  
    11. public class MyMonoBehaviour : MonoBehaviour
    12. {
    13.     void Awake()
    14.     {
    15.         MyObject obj = new MyObject();
    16.         Debug.Log("obj is null? " + (obj == null));
    17.     }
    18. }
    19.  
     
  2. Ntero

    Ntero

    Joined:
    Apr 29, 2010
    Posts:
    1,436
    Could be because UnityEngine.Object overrides the equals function to provide a special consideration for null;
    If you call MyObject.Destroy(); then MyObject == null will return true; even though the object is definitely still referencing an object in memory (until it gets specifically unreferenced or goes out of scope and the Garbage Collector gets it).

    This null comparison is allowed because equals is overridden so that an object after a Destroy() call will be comparable to null.

    Just a hunch though I'd think that you could still inherit from it without having to fiddle around with how it's instantiated.

    Edit: A Quick check with ILSpy shows that Equals(), ==, !=, and (bool) all use a Native code check rather than the default comparisons.
     
    Last edited: Jul 26, 2011
  3. DireLemming

    DireLemming

    Joined:
    Jul 8, 2011
    Posts:
    7
    Yup that's it, thanks Ntero.
     
  4. Ntero

    Ntero

    Joined:
    Apr 29, 2010
    Posts:
    1,436
    An ok solution when wanting to check if it's Really null without having to re-override all the overloading is the ReferenceEquals function.

    UnityEngine.Object obj = new UnityEngine.Object();
    Debug.Log(obj == null); //Returns true
    Debug.Log(ReferenceEquals(obj, null)); //Returns false
     
  5. DireLemming

    DireLemming

    Joined:
    Jul 8, 2011
    Posts:
    7
    Is there a restriction on how you can create UnityEngine.Object's at runtime? While the reference to the object is not null, the underlying engine representation is null. Attempting to set the name of this object will fail with a null reference exception.
     
  6. Ntero

    Ntero

    Joined:
    Apr 29, 2010
    Posts:
    1,436
    I'm not sure you can properly inherit from UnityEngine.Object. All child classes have their own Internal native code constructors, and you need to have some sort of native code data for that type of object to exist. Why is it you want to inherit from UnityEngine.Object, and are their any child classes (in particular look at ScriptableObject) that could suit your purpose?
     
  7. DireLemming

    DireLemming

    Joined:
    Jul 8, 2011
    Posts:
    7
    Needed a container class I could pass through AnimationEvent that holds onto a bunch of other data.
     
  8. DireLemming

    DireLemming

    Joined:
    Jul 8, 2011
    Posts:
    7
    Hmm, yeah looks like ScriptableObject is the only alternative. Was hoping to do it with less overhead, but guess that will have to do. Thanks.
     
  9. DomDom

    DomDom

    Joined:
    Oct 18, 2013
    Posts:
    33
    Well some years past...
    But anyways, I have a similar problem for an editor extension where I hold data on a seperate object. I derive from UnityEngine.Object, but I can't get the Undo.RecordObject() to work, because it says my Object is null...

    Okay, seems I have to switch to ScriptableObject, but if I understood that (http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/scriptable-objects) right, would you guys agree that I have to handle the creation of the ScriptableObjects by myself to avoid "leaking" the data object when it's created in every OnEnable() of the EditorScript.