• 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 xEnOnn · Sep 05, 2012 at 03:41 PM · getcomponent

Why do I have to use GetComponent() function?

I don't understand the usage of GetComponent() even after reading the documentation.

In an example I followed, they used transform.parent.GetComponent() in the script of a GameObject to access its parent.

Since every GameObject has a transform property, and suppose if I want to get something within the hierarchy of the GameObject called MyObject, I could just use transform.parent. But in the tutorial, they use transform.parent.GetComponent(), where ParentOfMyObject is the script name of MyObject's parent.

As transform.parent is going to be the same as GetComponent, why even have to use the function GetComponent? I feel that I am missing something out.

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

2 Replies

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

Answer by Fattie · Sep 05, 2012 at 03:47 PM

use GetComponent to get a SCRIPT that is attached to something

say you have something like en enemy ... happyEnemy

say there's a SCRIPT attached to happyEnemy, called 'MakeFire'

then,

happyEnemy.GetComponent(MakeFire)

that's how you get that SCRIPT (called 'MakeFire') which is attached to happyEnemy

OK ?

so if you want to get at a SCRIPT attached to anything, use GetComponent

(that will get you started - there are other ways to use it as well but that is 90% of the usage)

if you want to get at a SCRIPT attached to anything, use GetComponent

(They could have called it "GetScript" for the sake of making it easier for beginners to learn the whole system - heh! - since the vast majority of usage is to get a script, and as a beginner you'll only ever use it to get a script. Furthermore, as a beginner you're desperately trying to figure our "how the £$@ do I get a script?!" - indeed, that's the key insight, "GetComponent" encompasses the idea "Get A Script")

Note, this can also apply to "you". Say there is a hero. Say you are a script "EatOften" which is attached to the hero. Say there is ANOTHER script attached to the hero, perhaps "FindTrees". So you are EatOften. You want to get to FindTrees, in that case you simply say GetComponent(FindTrees)

Easy, right?

Note - in al cases, when you use GetComponent there are NO, repeat NO quotation marks around the name of the script inside the brackets.

Comment
Add comment · Show 5 · 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 · Sep 05, 2012 at 04:17 PM 2
Share

GetComponent should definitely not be called GetScript, since it GetsComponents. Not scripts. All scripts are components, but not all components are scripts. Some components, such as Transform, Rigidbody, etc., have shortcuts, so you can say blah.transform rather than blah.GetComponent(Transform), but many do not, such as $$anonymous$$eshFilter, ParticleAnimator, etc., so you need GetComponent if you're going to access them.

avatar image xEnOnn · Sep 05, 2012 at 04:28 PM 0
Share

@Fattie Thank you so much. You explanation is very very clear! I understand now. Thanks a lot! :)

avatar image Fattie · Sep 05, 2012 at 04:31 PM 0
Share

@agoxEnOnn it's nothing, really, I can only answer the simplest questions. the future belongs to you young people, enjoy it !

avatar image Eric5h5 · Sep 05, 2012 at 04:44 PM 1
Share

@Fattie: Calling a function something it is not, even (or especially) if it's "for the sake of beginners", doesn't make any sense even by your standard of lying to noobs. ;)

avatar image Eric5h5 · Sep 05, 2012 at 05:03 PM 0
Share

Or...I dunno...maybe you could just use GetComponent? And spend a $$anonymous$$ute or two learning what a Component is? (Since Components are kind of a big deal in Unity and the concept does need to be learned if you're going to do much of anything.) Or is that too easy....

avatar image
1

Answer by Kiloblargh · Sep 05, 2012 at 04:35 PM

Most of the time you don't have to use it, and indeed shouldn't use GetComponent if it's possible to assign the reference to the component (usually a script) in the inspector. If your object is already part of a parent-child hierarchy, and the script on one gameobject needs to access a component on another you can drag on the reference(s), make it a prefab and when you instantiate the prefab it will come into the world with the component already "got."

You only need to use it in situations where the component you're getting does not exist at the start of the game, for example, if you have a "DontDestroyOnLoad()" setup object that needs to persist through all levels:

 var scriptVar : ScriptName;
 var newThingy : GameObject;
 function OnLevelWasLoaded("_MyFirstLevel")
     {
     newThingy = GameObject.FindWithTag("GameThingy");
     scriptVar = newThingy.GetComponent.<ScriptName>() as ScriptName;
     }
Comment
Add comment · Show 2 · 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 · Sep 05, 2012 at 04:59 PM 0
Share

There's no need for this:

 scriptVar = newThingy.GetComponent.<ScriptName>() as ScriptName;

Just do this, it's the same thing but slightly faster (and shorter):

 scriptVar = newThingy.GetComponent(ScriptName);

@Fattie: er, no, assigning stuff in the inspector facilitates code re-use. If you hard-code stuff in the script, it's only useful for one thing. And assigning things in the inspector especially helps with moving stuff around. You can move stuff wherever the heck you want and you don't have to think about it--your reference in the inspector is always correct. If you hard-code things and then move them around, you have to remember to update the script.

avatar image Eric5h5 · Sep 05, 2012 at 07:46 PM 0
Share

You'll find working with Unity easier and more efficient if you work with it, not against it.

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

11 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

Related Questions

Accessing other objects efficiently 1 Answer

Destroy(GetComponent(...)) not working 1 Answer

GetComponent(); in Awake cant be accessed in Update 1 Answer

BCW0012: WARNING: 'UnityEngine.MeshCollider.mesh' is obsolete. mesh has been replaced with sharedMesh and will be deprecated 1 Answer

Problem getting information out of colliding objects 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