Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Franz · Jan 15, 2013 at 10:55 AM · c#objectcomponentnewallocation

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CodeMasterMike · Jan 15, 2013 at 11:08 AM 0
Share

What does the $$anonymous$$yButton class do?

avatar image kamil.slawicki · Jan 15, 2013 at 11:16 AM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;
 
 }
 }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiating an object returns null 0 Answers

Distribute terrain in zones 3 Answers

Enquiry on what are the maximum number of components for a specific object in Unity 1 Answer

Enquiry on what are the maximum number of components for a specific object in Unity 0 Answers

Multiple Cars not working 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges