• 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 24, 2012 at 09:50 PM · addcomponent

AddComponent argument in quotes vs without

What is the difference between something like AddComponent("Collider") and AddComponent(Collider) - what do the quotes do exactly?

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
1

Answer by aldonaletto · Jan 24, 2012 at 11:53 PM

The "unquoted" version works for javascript too, and is easier than in C#: AddComponent(Collider). It's better to use the unquoted version whenever you can: it's faster, and the compiler checks if the type exists.
The string version should be used only when you need to select the type to add at runtime, or when the type doesn't exist at compile time - this may happen when you want to add a script written in a different language, because C# and JS can't see each other during compilation. If you want to add the script "PlayerStatus.cs" to an object in a JS script, for instance, you may write AddComponent("PlayerStatus") - AddComponent(PlayerStatus) will not work unless PlayerStatus has been previously compiled.

Comment
Add comment · Show 3 · 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 Eric5h5 · Jan 25, 2012 at 12:18 AM 0
Share

JS scripts can see C# scripts, and vice versa, depending on compilation order. e.g. "foo.cs" in Standard Assets will be visible to JS scripts outside of Standard Assets (but not inside).

avatar image ina · Jan 25, 2012 at 12:35 AM 0
Share

Does this mean that at runtime, you should call always AddComponent("Collider") ins$$anonymous$$d of AddComponent(Collider)?

avatar image syclamoth · Jan 25, 2012 at 01:02 AM 1
Share

No, that's not what he means at all. You should only call 'AddComponent("Collider")' if you don't know at compile time what the type will be.

avatar image
1

Answer by Eric5h5 · Jan 25, 2012 at 12:31 AM

In JS, AddComponent("Collider") returns Component, whereas AddComponent(Collider) returns Collider, plus the non-string version runs faster, and if you make a typo, the compiler will catch it.

In C#, AddComponent("Collider") and AddComponent(typeof(Collider)) both return Component, though the non-string version still has the advantage of being faster and more typo-proof. In order to actually get Collider instead of Component, you can cast: AddComponent(typeof(Collider)) as Collider, or use the generic version, AddComponent<Collider>(), though the generic version is slightly slower.

You can use the generic version in JS, AddComponent.<Collider>(), which returns Collider, however there's no point, since it does the same thing as the non-generic version, except it's uglier and slightly slower.

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 Techfix · Jan 24, 2012 at 11:40 PM

The quotes are for passing in by the name (as a string) of the script instead of the by it's type. According to the script reference, the second one (without quotes) is for C# users. They use the generic method:

 AddComponent<Collider>();

JavaScript users use:

 var c : Collider;
 c = gameObject.AddComponent ("Collider");

Provided that you adhere the convention that the type is the same as the script name, then it shouldn't matter which one you use if you are using JavaScript. Though I never code in Boo, it seems to support both. In my experience with C# only the generic method works.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

8 People are following this question.

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

Related Questions

Loading Scripts onto GO before in-game scene 1 Answer

Adding Prefab Components 1 Answer

Will using AddComponent frequently lower performance? 1 Answer

Error in AddComponent 1 Answer

How can I add external-assets(C#) 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