Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 $$anonymous$$ · Jun 06, 2015 at 03:21 PM · c#rigidbodynullgenerics

Why is my Rigidbody != null immediately after seeing that is null?

I am checking to see if "My Game Object" has a Rigidbody. It does not. But the conditional for the null check on Rigidbody fails, despite having just been proven null.

Why is this happening? How can I make my condition block run?

 using UnityEngine;
 using System.Collections;
 
 public class NullChecker : MonoBehaviour
 {
 
     void Start()
     {
         GameObject go = GameObject.Find("My Game Object");
         CheckIfNull<Rigidbody>(go);
     }
 
     public void CheckIfNull<ComponentType>(GameObject gameObject)
     {
         ComponentType component = gameObject.GetComponent<ComponentType>();
         Debug.Log("Component is " + component); //"Component is null"
         if (component == null)
         {
             Debug.Log("Inside null check"); //Never prints
         }
         Debug.Log("Finished null check"); //Does print
     }
 
 }
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

1 Reply

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

Answer by Mouton · Jun 06, 2015 at 03:41 PM

Because your generic type "ComponentType" is not Unity-nullable, as it can pass non-nullable types (you can pass a struct, which is a non-nullable type).

You need to change

 public void CheckIfNull<ComponentType>(GameObject gameObject)

with

 public void CheckIfNull<ComponentType>(GameObject gameObject) where ComponentType : Component

If you want you generic to allow non-nullable type, you can do this

Change

 if (component == null) // Doesn't work

with

 if (component.GetType() == typeof(ComponentType)) // Works

Hope this helps !

[Edit:] Since you are testing for a component, you should use this syntax:

 public void CheckIfNull<ComponentType>(GameObject gameObject) where ComponentType : Component

Otherwise, you could pass any type, even something which is not a component, like an int.

 CheckIfNull<int>(go); // the compiler won't warn, it will result in an ArgumentException

You will be able to pass MonoBehaviours too, as they derive from Component.

Comment
Add comment · Show 2 · 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 $$anonymous$$ · Jun 06, 2015 at 03:44 PM 0
Share

Awesome! I ended up switching component == null to component.Equals(null). Are there any unwanted side effects from this?

avatar image Mouton · Jun 06, 2015 at 04:02 PM 1
Share

There are, but it depends of your needs. If somebody (you, Unity developpers, or any pluggin you use) decide to override the Equals method of the Type you try to pass, the new method will get called, resulting in undesired behaviour.

Yet, if you know that it will never happen, there is no issue with it.

For more security, you can use the Generic comparer:

 if (EqualityComparer<ComponentType>.Default.Equals(component, default(ComponentType)))
 {
     // [...]
 }

For further details, look at this answer.

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

20 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Collision triggers do not work on child gameobject 3 Answers

GetComponent keeps returning null 3 Answers

Problem with jumping in fps game 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