Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 StrongGuy · Oct 24, 2015 at 05:25 AM · script.monobehaviourunity 4.6

UnityEngine.Object is null but System.Object is NOT null!

 public class JJJ : MonoBehaviour
 {
     void Start ()
     {
         Component com = gameObject.GetComponent(typeof(UnityEngine.SkinnedMeshRenderer));
 
         if (com == null)
             print("com == null");
         else
             print("com != null");
 
         Print (com);
     }
     
     void Print(object obj)
     {
         if (obj == null)
             print("obj == null");
         else
             print("obj != null");
     }
 }

Output:

 com == null
 obj != null.


Why is com null but obj is not null?

This gameObject does not have SKinnedMeshRenderer attached.

Thank you

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Bunny83 · Oct 24, 2015 at 07:32 AM

There are already tons of threads and questions on that topic:

  • Official post from Lucas Meijer

  • http://forum.unity3d.com/threads/fun-with-null.148090/

  • http://answers.unity3d.com/questions/524998/testing-for-null-bug-or-feature.html

  • http://forum.unity3d.com/threads/null-check-inconsistency-c.220649/

  • http://answers.unity3d.com/questions/731469/non-null-object-with-working-properties-evaluates.html

  • http://answers.unity3d.com/questions/1040391/what-so-many-bizarre-designs.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
1

Answer by SniperEvan · Oct 24, 2015 at 05:47 AM

Could be because UnityEngine.Object overrides the equals function. I know when you call Destroy (object) and then check for null Unity returns null even when the object hasn't been cleared from memory yet. My point is that unity is doing some extra management and I think if the object exists it doesn't return null.

I'm not sure if that helps.

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 Matheuz · Apr 07, 2020 at 07:50 PM

In a summary: there are 2 objects that live in different worlds: a C# script (your code) and its engine (native) components, like transform and gameObject. The lifetime of the C# objects is determined by the garbage collector and it won't point to null unless you explicitly do it. The lifetime of the engine components is determined by calls to Destroy. Thus, a MonoBehaviour's engine components are destroyed when you call OnDestroy, but its C# object will live as long as you keep a reference to it. This article explains it better.

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

33 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Do you tend to have scripts to mark prefabs 1 Answer

Disable all scripts on gameobject excluding the one calling for disable 2 Answers

I'm trying to display my ammo; not working 2 Answers

generic / templatized components / monobehaviors ? 0 Answers

Call Editor function from Monobehaviour script 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