• 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 ComeSweetDeath · Sep 14, 2014 at 11:30 PM · getcomponent

How come I don't have to type "GetComponent(transform)()" but instead I can just type "gameObject.transform"?

 using UnityEngine;
 using System.Collections;
  
 public class testTransform : MonoBehaviour {
  
     public Transform tran;
  
     void Start () {
         tran = GetComponent<Transform>();
     }

How come I don't have to type "GetComponent(transform)()" but instead I can just type "gameObject.transform"?

Is it because of MonoBehaviour or UnityEngine that's allowing me to take this "short cut"? Thanks!

Comment
Add comment · Show 2
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 ComeSweetDeath · Sep 14, 2014 at 11:34 PM 0
Share

(Btw sorry for the title, UnityAnswers won't let me use angle brackets)

avatar image ComeSweetDeath · Sep 15, 2014 at 01:50 AM 0
Share

Thank you for your answers! (I can't mark both as answers unfortunately :( )

2 Replies

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

Answer by Benproductions1 · Sep 14, 2014 at 11:51 PM

The Component class has getters for most of the built-in components. Quite literally:

 public class Component : Object {
     public Transform transform {
         get { return GetComponent<Transform>(); }
     }
     // etc
 }

Note, that MonoBehaviour is a class inside of the UnityEngine. MonoBehaviour inherits from Components. So to answer your question, it's both because of MonoBehaviour, UnityEngine as well as Component.

Also note, that GetComponent is actually an alias for gameObject.GetComponent. A bunch of functions are like this and exist purely to make things a little bit easier.

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 rutter · Sep 14, 2014 at 11:45 PM

Unity's MonoBehaviour class includes a few properties that pretty much syntactic sugar for quick GetComponent calls. Something roughly like this:

 public Transform transform {
     get {
         return GetComponent<Transform>();
     }
 }

Several similar properties exist to look up an attached AudioSource, Camera, Renderer, Rigidbody, or so on. See the scripting reference for more details.

Any behavior script you write must inherit from MonoBehaviour, so most of your scripts will also have access to these shortcuts.

If you do decide to write your own helpers, I recommend adding a cache step to avoid unnecessary extra calls to GetComponent.

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 15, 2014 at 02:01 AM 5
Share

But if you are planning on using Unity 5, it would be best to forget about all the shortcuts, except transform. Use GetComponent for everything else. http://blogs.unity3d.com/2014/06/23/unity5-api-changes-automatic-script-updating/

avatar image Bunny83 · Oct 27, 2014 at 03:25 PM 0
Share

@Eric5h5: Just read that comment (because the question got bumbed for some reason).

That actually sound great :D. So Unity finally is cleaning out their API which has grown quite uncontrolled over the years. It has so many inconsistencies which just confuses new users. This question here actually proves that. It's better to have a consistent way ins$$anonymous$$d of having lots of small specialized "helpers".

The new UI system seems to rely more on common OOP concepts, but i doubt they will change some of the old concepts to be more OOP like. It would be great to have more "hooks" in the pipeline. Like in the physics system for example. WaitForFixedUpdate is nice, but it would be great to have callbacks for collisions which allows you to influence the collision response. All those "points" where coroutines can hook in should also be available either as "Application.delegate" or "$$anonymous$$onoBehaviour.delegate" (or both). That would give a lot of flexibility.

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

25 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

Related Questions

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

Why do I have to call ToString() when fetching a String from another script? 2 Answers

GetComponent null reference C# 1 Answer

How can I access an inherited method from a separate (collided) object? 1 Answer

getting a variable from another script problem 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