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 FatallErroR · Jul 26, 2013 at 11:31 PM · javascriptrigidbodyifcheck

Check if object is Rigidbody .js

Hello guys,

I wanna know what's the term to check if the script object is Rigidbody or not.

i've tried this piece of code but, sadly, it doesn't work

 if (gameObject.GetComponent (Rigidbody) == true) { 
      doStuff();
     }

Thanks in advance.

Please don't swear, this is a G-Rated 'site. Alucard Jay

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

2 Replies

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

Answer by AlucardJay · Jul 26, 2013 at 11:48 PM

Store the reference in a temporary variable, then check if that variable is null

 var rBody : Rigidbody = GetComponent( Rigidbody );
 
 if ( rBody ) // same as writing if ( rBody != null )
 {
     DoStuff();
 }
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 FatallErroR · Jul 27, 2013 at 06:43 AM 0
Share

Thank you very much Alucard, and sorry for swearing ^^ also, how's Richard Belmont? he's doing fine?

avatar image AlucardJay · Jul 27, 2013 at 08:20 AM 0
Share

I'm Hellsing, Integra is fine thanks. Well spotted, most people don't see it =]

avatar image
0

Answer by Linus · Jul 27, 2013 at 07:10 AM

Rigidbody actually has a magic shotcut.

You can simply do this:

 if(rigidbody){
     Debug.Log('I have a rigidbody');    
 } else {
     Debug.Log('I do not have a rigidbody');    
 }


You can access rigidbody on other objects through transformVar.rigidbody

Comment
Add comment · Show 4 · 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 AlucardJay · Jul 27, 2013 at 08:23 AM 0
Share

This is correct, but I went with the code the OP had. It also helps to understand the gameObject-Component relationship when trying to access components that are not variables of gameObject.

avatar image Linus · Jul 27, 2013 at 08:30 AM 0
Share

No problem, just providing extra information. And if I recall correctly GetComponent is done when using transform.rigidbody, so if its used in Update() it should be copied to a variable like you did.

avatar image AlucardJay · Jul 27, 2013 at 08:33 AM 0
Share

Absolutely! Lots of good information here for future readers =]

avatar image FatallErroR · Jul 28, 2013 at 06:39 AM 0
Share

Thank a lot guys, it'll be very usefull in the future for me.^^

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

17 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

Related Questions

Ball getting stuck in surfaces and bouncing for no reason? Help! 0 Answers

How to check if a function is called? 2 Answers

How can I check if another android app is running? 0 Answers

Checking and saving the size of gameObject! [Javascript] 0 Answers

Increasing a Variable on Collision 2 Answers


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