Verify if a Component is null

Well, that’s strange.
I state I’m not a c# expert , but in C++ to verify if something is correctly created I use the

if (Obj==NULL)
{do something}.

I write a class based on Component , and the class is working , I see the component ad runs perfectly.
Second step , the automatic creation.
The program have a piece of source as the following

//the creation. It's working , I'm sure!
B=new MyButton("Ciao!","Imm/a_001","Imm/a_001bis",10,20,100,50);
//the verify (and the problem)
if (B==null)
   Debug.Log("I don't understand");

and the program tell me that the object is null.
WHY?
And , more useful ,
HOW CAN I VERIFY IF THE OBJECT IS CORRECTLY CREATED?
Or I have to assume the c# magically doesn’t full the RAM if I create too much object?

You do not create MonoBehaviour inheriting classes by the new keyword.
You do so by adding a component, while the result is the created instance:

Also, check out the answer I gave just a few days ago:

First of all you check if an object has been successfully created like this:

if (obj != null)
{
    // success
}

It’s the same as in C++ btw. I guess you ment this but you wrote the oposite.

Second, you must not derive a class from any Unity class except:

  • MonoBehaviour
  • ScriptableObject

In the editor additionally:

  • Editor
  • EditorWindow (which includes ScriptableWizard)
  • AssetPostprocessor
  • AssetModificationProcessor

Next thing is that all classes derived from Component (built-in or your own MonoBehaviour classes) can’t have a custom constructor and must not created with “new”. The only way to create a component is to instantiate an existing GameObject which will clone the GO with all components, or to use AddComponent(). Components can’t exist on their own, they need to be attached to a GameObject.

Last thing: Almost all Unity classes have a native code part and a managed part. So if you have an object in Untiy, it will exist in the c++ native part of the engine as well as a managed C# / .NET class. They are kind of linked together. When you use Destroy on an instance the native part will be destroyed and the managed part will be “marked as destroyed”. You can’t destroy a class in .NET since it’s a garbage collected language.

Unity has overloaded the == and Equals operator to “pretend” a reference to a destroyed object is null because you can’t use it anymore when you have destroyed the native part already. When you create a class with “new”, the class isn’t attached to a GameObject.

Any Unity Component Check null or not.

  1. Create Script :
    InternalComponentExtension.cs

    public class InternalComponentExtension : MonoBehaviour

     {
         [SerializeField]private static TextMeshProUGUI textMeshProUGUI;
         [SerializeField]public static TextMeshProUGUI TextMeshProUGUI
         {
             get{
                 if(textMeshProUGUI == null)
                 {
                     GameObject g1 = new GameObject();
                     textMeshProUGUI = g1.AddComponent<TextMeshProUGUI>();
                 }
                 return textMeshProUGUI;
             }
         }
        
         [SerializeField]private static MeshRenderer _renderer;
         [SerializeField]public static MeshRenderer Renderer
         {
             get{
                 if(_renderer == null)
                 {
                     GameObject g1 = new GameObject();
                     _renderer = g1.AddComponent<MeshRenderer>();
                 }
                 return _renderer;
             }
         }
     }
    
  2. Access or Check Component
    Create Script :
    Demo.cs and add in to any GameObject in scene.

    public class Demo: MonoBehaviour
    {

     [SerializeField]private TextMeshProUGUI cookingNote;
     [SerializeField]private TextMeshProUGUI CookingNote
     {
         get
         {
             if(cookingNote == null)
             {
                 cookingNote = InternalComponentExtension.TextMeshProUGUI;
             }
             return cookingNote;
         }
     }
    
    
    [SerializeField]private Renderer  myRenderer  ;
     [SerializeField]private Renderer  MyRenderer
     {
         get
         {
             if(myRenderer  == null)
             {
                 myRenderer  = InternalComponentExtension.**Renderer** ;
             }
             return myRenderer  ;
         }
     }
    

    void Start()
    {
    //Access any Renderer or TextMeshProUGUI property.
    CookingNote.text = “It is Null”;

        MyRenderer.enabled = false;
        MyRenderer.rendererPriority = 1;
    
        Button.interactable = false;
    

    }
    }