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
1
Question by RoR · Feb 17, 2011 at 06:18 PM · getcomponentscriptingbasics

What's the difference between the two GetComponents?

I am using C#.

GetComponent("STRING") vs GetComponent<Obj>();

What's the main difference between the two?

I typically use this method like so:

GS = (GameSystem)GameObject.Find("Manager").GetComponent("GameSystem");
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

1 Reply

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

Answer by KaiseanGames · Feb 17, 2011 at 06:35 PM

If you are using only C# in your scripts, there is no functional difference. The thing is, comparing types is faster than comparing strings, so generic method GetComponent<T>() is faster than GetComponent(String).

However, since javascript does not support generics, there is no way to fetch components created as a .js file using GetComponent<T>().So, if you need to get components from JS files, use GetComponent(String), for example GetComponent("MyComponent"). EDIT: Jessy corrected me - it seems that using generics in Unity's JS is a possibility. Anyhow, I made a small test of performances. It seems that average call for generic GetComponent<T>() is about 10 times faster than its non-generic type. (10 millions calls in 2.45 seconds in contrast to 25 seconds on non-generic method).

Also, there is another benefit of using generic method - readability. It does not require a cast operator.

Like I said, if you are using only C# - stick to GetComponent<MyComponent>(). If you are mixing script languages I'd go for string version and call it as few times as possible.

On a side note, consider making a reference (private variable) to the Manager in your script and get the component only once (not each time you are using it) because GameObject.Find() is extremely slow to be called too often (especially not in *Update() functions).

Comment
Add comment · Show 1 · 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 Jessy · Feb 17, 2011 at 06:38 PM 1
Share

Unity's JavaScript has supported generics for a while now. Syntax is GetComponent.<$$anonymous$$yComponent>() - Just add a dot.

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

Access collider custom script's variables 1 Answer

trying to use get component with find game object: error CS0131: The left-hand side of an assignment must be a variable, a property or an indexer 1 Answer

Accessing variables in another script using GetComponent() 0 Answers

Trigger animation 2 Answers

best method for high volume script management 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