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 Showken · Oct 13, 2010 at 06:09 AM · getcomponentaddcomponentgenerics

How should I use the Generic versions of GetComponent<T> and AddComponent<T>?

How should I use the Generic versions of GetComponent and AddComponent?

Comment
Add comment · Show 4
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 duck ♦♦ · Oct 13, 2010 at 08:57 AM 0
Share

Are you asking about the generic versions of GetComponent and AddComponent? if so, edit your answer to include this, and you'll probably have the negative votes changed to positive (I hope!)

avatar image PeterDC · Oct 13, 2010 at 09:15 AM 0
Share

@Duck: Did you mean "if so, edit your question"?

avatar image duck ♦♦ · Oct 13, 2010 at 09:19 AM 0
Share

Yes, I meant edit your question, not answer :-)

avatar image Showken · Oct 13, 2010 at 02:33 PM 0
Share

i'm sorry !it's GetComponent

3 Replies

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

Answer by duck · Oct 13, 2010 at 08:56 AM

Fromt the <T> mentioned in the title, I assume Showken is asking specifically about the Generic versions of the component commands, namely:

  • GetComponent<T>
  • AddComponent<T>

These are available in C# (and Unity's Javascript since Unity 3.0), and they are useful in C# because they avoid the need for the "typeof" keyword when specifying the type, and having to cast the result which is returned as an Object by the non-generic versions of these methods.

For example, without the Generic versions, these commands look like this written in C#:

Enemy enemy = (Enemy) GetComponent(typeof(Enemy));
bullet = (ProjectileScript) projectile.AddComponent(typeof(ProjectileScript));

Contrasted with the Generic versions, which look like this:

Enemy enemy = GetComponent<Enemy>();
bullet = projectile.AddComponent<ProjectileScript>();

Much nicer!

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 burnumd · Oct 13, 2010 at 01:49 PM 0
Share

Generics actually do work in Javascript with Unity 3.0, but you must use a "." between the function/constructor name and the angle brackets, ie. "GetComponent. ()" The change is described in http://unity3d.com/support/documentation/$$anonymous$$anual/$$anonymous$$onoUpgradeDetails.html

avatar image duck ♦♦ · Oct 13, 2010 at 03:15 PM 0
Share

Wow, nice! Hadn't read that yet. I'd better update my old wiki pages and blog posts on the subject!

avatar image Showken · Oct 14, 2010 at 02:06 AM 0
Share

thanks!You are all so enthusiastic.

avatar image Showken · Oct 14, 2010 at 03:01 AM 0
Share

I am sorry!there are some problem I don't understand!follow three codes: 1: Enemy enemy = (Enemy) GetComponent(typeof(Enemy)); 2: Enemy enemy = GetComponent(); 3: Enemy enemy=new Enemy();; what is the different?

avatar image
0

Answer by SrBilyon · Oct 13, 2010 at 08:41 AM

Check here for how to use "Component" in scripting: http://unity3d.com/support/documentation/ScriptReference/Component.html

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
0

Answer by Ashkan_gc · Oct 13, 2010 at 08:45 AM

as i know there is not any usecase that you might want to use it. you can create script components or create components like (Rigidbody) and call AddComponent to add the created component to a gameObject but i can not understand why you should use Component alone.

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

No one has followed this question yet.

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to GetComponent from Class that uses Generics 1 Answer

Type of a script 1 Answer

Reference Not Set toInstance of Object 1 Answer

Sprint Script 3 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