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 ina · Jan 25, 2012 at 01:15 AM · gameobjectrigidbodydestroyaddcomponent

Difference between capitalized component in AddComponent and Destroy

I am still not sure what the difference is between

AddComponent(Rigidbody) and AddComponent(rigidbody) ???

It seems that if you add component a Rigidbody (capitalized), you can reference it as rigidbody (lowercase) automatically.

But, Destroy(rigidbody) will generate a syntax error...

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
1

Answer by syclamoth · Jan 25, 2012 at 01:45 AM

Well now. You're talking about a few different things here.

The capitalised 'Rigidbody' is the typename, and the lowercase 'rigidbody' is the automatic lookup value in the Component class which automatically returns the first found instance of the class 'Rigidbody'. The important difference here is the usual best-practice one-

UpperCase for classes,

lowerCase for objects.

Did you actually try using AddComponent(rigidbody)? Since 'rigidbody' is not a valid type, I somehow doubt that that would work at all.

The automatic lookup for 'Rigidbody' components is something set up for you by the base classes all JS scripts derive from. It's not magical, and neither is it a pattern: for example, you could create a script 'MyComponent' and use AddComponent(MyComponent) to add one to your object, however, you would not then be able to refer to it automatically as 'myComponent' without first using the line

 var myComponent : MyComponent = GetComponent(MyComponent);

Which, of course, manually defines it.

As for the last part, I really don't see what's going on.

 Destroy(rigidbody);

should only return an error if there is no rigidbody already on the object, which is to say, nothing to destroy. Otherwise, it should just destroy the first Rigidbody-type component that it can find.

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 luizgpa · Jan 25, 2012 at 01:49 AM

Rigidbody is a type. rigidbody is a "reference" to a Rigidbody attached to the GameObject. Actually this code

 var body = rigidbody;

is equivalent to this one

 var body : Rigidbody = GetComponent(Rigidbody);


AddComponent requires a type as parameter, while Destroy requires a reference to an object.

I know that UnityScript syntax sometimes changes between versions, but in 3.2 both AddComponent(rigidbody) and Destroy(Rigidbody) show an error (No appropriate version of 'XXX' for the argument list '(YYY)' was found), while AddComponent(Rigidbody) and Destroy(rigidbody) are fine (syntax-wise).

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

6 People are following this question.

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

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

"Some objects were not cleaned up when closing the scene" 1 Answer

How can I destroy a GameObject from a Rigidbody variable? 1 Answer

Why doesn't this script work?? Please help.. 1 Answer

Clearing a LineRenderer Whiteboard, so that you can start drawing again 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