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 MCJOHNS117 · Mar 02, 2013 at 08:39 PM · gameobjectgetcomponentmonobehaviourpropertiesshared

Monobehaviour shared properties across single gameobject

I have a few classes that are derived from Monobehaviour attached to the same GameObject and I was curious to the nature of the GetComponent calls.

If I call GetComponent().transform and GetComponent().transform, my testing has shown that they are the same, even when I do not adjust the values in ComponentB. So is the transform stored once in the GameObject, and accessed by all monobehaviours attached to that game object?

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
0

Answer by Intensity3000 · Mar 02, 2013 at 09:27 PM

As to the transform, I believe transform = GetComponent(Transform). When I use the GetComponent function for example it is often when i am creating a reference to another script and its variables. For example I have the script for the player character, PlayerScript, and the WeaponScript. I want to access the damage variable in the weapon script from within the PlayerScript, so in the PlayerScript I would type a variable to the weapon gameobject itself, and the code to create a reference to the script and its variable for damage would be something like this

var damage = weapon.GetComponent(WeaponScript).damage

this would establish a reference to the damage variable within the weapon script, accessible from the character script

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 MartinCA · Mar 02, 2013 at 09:35 PM

You are correct, Transform is actually a type of component attached to a GameObject, the only unique thing about Transform is that it is a mandatory component on all GameObjects and you cannot disable or remove it.

That said, all classes deriving from Component ( including Transform and Behaviour/MonoBehaviour which are your scripts ) store quick accessors to common components such as .transform ( see Component Reference Page ).

So in reality, assuming you have a GameObject with Transform component, ScriptA and ScriptB attached to it every reference within those components will point to the said Transform component:

 // These are synonymous
 transform == GetComponent< Transform >()
 
 // As are these
 GetComponent< Transform >() == GetComponent< ScriptB >().transform
 
 // And you can even reference loop to infinity!
 transform == transform.transform.transform.transform.transform

Hope this helps!

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 whydoidoit · Mar 02, 2013 at 09:51 PM 0
Share

"Quick accessors" in terms of typing, but not in terms of performance - it's always worth caching transform, animation etc.

avatar image MCJOHNS117 · Mar 02, 2013 at 09:58 PM 0
Share

This is exactly what I was looking for! I had thought that was the case from what my testing had shown, thank you for confir$$anonymous$$g my suspicions!

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

12 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

Related Questions

Adding Arbitrary Properties to GameObjects 1 Answer

Creating a single-line function for GameObject.Find and GetComponent (for multiple components) 3 Answers

Get components of multiple instantiated objects? 2 Answers

GetComponent vs AddComponent 3 Answers

How to get a variable value from another script(C#)? 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