• 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 /
This question was closed Jul 18, 2017 at 05:57 PM by agencebrunel for the following reason:

The question is answered, right answer was accepted

avatar image
2
Question by agencebrunel · Jul 18, 2017 at 04:59 PM · addcomponent

Addcomponent is obsolet, what should i do ?

Hi, doc said that addcomponent is obsolet. https://docs.unity3d.com/ScriptReference/GameObject.AddComponent.html

  • I want to add colider to my prefab to detec colision, in js.

Thanks

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

  • Sort: 
avatar image
7
Best Answer

Answer by Dave-Carlile · Jul 18, 2017 at 05:49 PM

Adding a component by name is obsolete. It's "bad" because it isn't type safe - it can't be validated at compile time.

As others have said, you can pass in the type. However the other answers are showing you incorrect syntax for doing so. The documentation you referenced shows the proper syntax:

 void Start()
 {
     SphereCollider sc = gameObject.AddComponent(typeof(SphereCollider)) as SphereCollider;
 }

In other words, since you're passing in a type you must use the typeof function to get the type.

The other way to do this is with generics, and this also has an example in the documentation. This is arguably the "best" way.

 void Start()
 {
     SphereCollider sc = gameObject.AddComponent<SphereCollider>() as SphereCollider;
 }
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
3

Answer by SolomonG · Jul 18, 2017 at 05:15 PM

If you were to do gameObject.AddComponent("Rigidbody"), that would be obsolete and not recommended. However, if you were to do gameObject.AddComponent(Rigidbody) that would not be. The first AddComponent function takes a string as its argument, which is the name of the class that will be added, in this case "Rigidbody". The second takes a type as its argument, which is the actual type reference to the class that will be added. The second method is much more robust and less prone to failure. You can still use AddComponent, but make sure you're passing in the type instead of type name (SphereCollider vs "SphereCollider").

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
2

Answer by Reynarz · Jul 18, 2017 at 05:17 PM

In the docs says: AddComponent with string is obsolete, instead use AddComponent with a type.

Example:

   //Good, not obsolete.
   function Start() {
     var sc: SphereCollider = gameObject.AddComponent(SphereCollider) as SphereCollider;
 }

   //Bad, obsolete.
   function Start() {
     var sc: SphereCollider = gameObject.AddComponent("SphereCollider");
 }

SpereCollider is a type. you need put the type in the method paramenter, not the string. In c# would be typeof(MyType);

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

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

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

Related Questions

Football free kick question? 1 Answer

AddComponent with exposed 1 Answer

Variables assigned in the inspector of a script don't seem to work when instantiating them via AddComponent 0 Answers

Disable "Performing a potentially slow search for component..." 1 Answer

how can i add another scripts functionality to a button on another script? 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