• 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 burnleyboy101 · Jun 20, 2013 at 06:06 PM · transformgetcomponent

Correct use of GetComponent(), Find(), etc.

I've seen that their is a lot of ways to use the brackets in transform. extentions such as GetComponent(). Sometimes I hear how you should use quotation marks, no quotation marks, brackets, etc. So I just want to know once and for all: What is the diffrence between:

 transform.GetComponent(x);
 
 transform.GetComponent("x");
 
 transform.GetComponent(x());

and any other way of using it.

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
Best Answer

Answer by Eric5h5 · Jun 20, 2013 at 08:01 PM

Assuming Unityscript:

 transform.GetComponent(x);

This returns the component of type x.

 transform.GetComponent("x");

This returns Component. Do not use this method.

 transform.GetComponent(x());

This method doesn't exist and would be a syntax error.

 transform.GetComponent.< x >();

This returns the component of type x. It works exactly the same as GetComponent(x) but is slightly slower. Don't use this in Unityscript; the only reason you'd use this is if you were using C# and wanted shorter code that doesn't require casting.

So really, the only thing you ever need in Unityscript is GetComponent(x). Everything else is worse in some way. C# is more complicated, as mentioned above, since in that case GetComponent(x) returns Component and requires casting.

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 oliver-jones · Jun 20, 2013 at 06:21 PM

Within JavaScript, I always use this:

 transform.GetComponent(ScriptName);

And it works fine, your last one 'GetComponent(x()); looks like when calling a function within that script:

 transform.GetComponent(ScriptName).FunctionName();

I never use quotation marks, but I don't think it makes a difference.

---- Additional ----

As for the Find, it works the same way only difference is that it is a little bit more heavier on Unity, as the Find will search through EVERY game object within the scene to find a match. Whereas the GetComponents, you have already identified the gameObject/transform in which you want to grab, it simply searching for a component. Example:

 parent.GetComponent(Health);

Will search ONLY on the parent of the transform for a component, like a script called 'Health'.

 GameObject.Find("Health");

Will search for a game object within the scene called 'Health', but might not necessarily have a Health script on it

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 robertbu · Jun 20, 2013 at 06:41 PM 0
Share

I never use quotation marks, but I don't think it makes a difference.

The non-quote version is faster, plus, if you get the type wrong, it gives you compile time errors rather than runtime errors.

avatar image
0

Answer by tyjkenn · Jun 20, 2013 at 06:25 PM

transform.GetComponent(type) is best for performances reasons. Sometimes, passing a string would be necessary, like when referencing a C# script from JavaScript, but it should be avoided where possible. http://docs.unity3d.com/Documentation/ScriptReference/GameObject.GetComponent.html

The other way is generic functions. An example would be GetComponent< Rigidbody >();. This is only really helpful in C#, where you would need a cast and the typeof function otherwise: (Rigidbody)GetComponent(typeof(Rigidbody));

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 AntiLunchBox · Jun 20, 2013 at 07:44 PM 1
Share

in c# you can just do

.GetComponent < Custom Script >();

avatar image tyjkenn · Jun 20, 2013 at 08:14 PM 0
Share

That's what I meant. Unity answers wont let me put greater than or less than. It must recognize it as a tag. How do I escape it?

avatar image Eric5h5 · Jun 20, 2013 at 08:27 PM 0
Share

Do what AntiLunchBox did and use spaces.

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

18 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

Related Questions

modifing enemies target inside a radius 1 Answer

How getting transform of another GameObject C#? 1 Answer

How to use "GetComponent" and "transform" code in dll file? 0 Answers

Endless 3D plane repetition animated by script is not moving 0 Answers

Why won’t GetComponent ().rotation.SetLookRotation actually change the object’s rotation? 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