- Home /
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?
From what you said I understand that $$anonymous$$yButton derives from $$anonymous$$onoBehaviour, is that true? If so you should use AddComponent http://docs.unity3d.com/Documentation/ScriptReference/GameObject.AddComponent.html ins$$anonymous$$d of the new keyword and have an Initialize function that does all your set-up for you. And yes, you can check if a component exists by checking if it is null, I am not 100% sure if it works in all cases but I tend to use pretty much everywhere and it's working fine.
As of verifying if a GameObject is correctly created just check if the reference you have to your GO is null or not.
Answer by GuyTidhar · Jan 15, 2013 at 11:19 AM
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:
http://docs.unity3d.com/Documentation/ScriptReference/GameObject.AddComponent.html
Also, check out the answer I gave just a few days ago:
http://answers.unity3d.com/questions/379662/variable-from-other-class-always-returns-0.html
Answer by Bunny83 · Jan 15, 2013 at 11:26 AM
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.
Answer by atulvi · Dec 16, 2020 at 06:08 PM
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.[B]Renderer [/B];
}
return myRenderer ;
}
}
void Start()
{
//Access any Renderer or TextMeshProUGUI property.
CookingNote.text = "It is Null";
MyRenderer.enabled = false;
MyRenderer.rendererPriority = 1;
Button.interactable = false;
}
}
Your answer
Follow this Question
Related Questions
Enquiry on what are the maximum number of components for a specific object in Unity 1 Answer
Multiple Cars not working 1 Answer
Enquiry on what are the maximum number of components for a specific object in Unity 0 Answers
Distribute terrain in zones 3 Answers
Instantiating an object returns null 0 Answers