Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
2
Question by brandon1 · May 10, 2011 at 06:25 PM · optimizationgetcomponentproperties

Are .transform .gameObject .rigidbody component calls or just pointers?

I'm trying to avoid GetComponent calls in my update function because they seem to slow things down when too many are called at once. I've gotten used to just keeping variables that store the components I need in every script. My question is, do .transform .gameObject .rigidbody all count as a .GetComponent...etc or is it just a pointer. If it's a pointer, I'll just call it directly in the update script.

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

Answer by Eric5h5 · May 10, 2011 at 07:06 PM

Yes, .transform and .rigidbody are basically the same as using GetComponent(Transform) and GetComponent(Rigidbody). gameObject isn't, because a gameobject isn't a component anyway.

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 brandon1 · May 10, 2011 at 07:13 PM 0
Share

Yea not sure why I put gameObject in there. So storing my own reference will be faster then?

avatar image Peter G · May 10, 2011 at 07:23 PM 1
Share

Yes and equally important it allocates less memory.

avatar image Jean-Fabre · May 11, 2011 at 06:05 AM 0
Share

uhm, did not know that .transform was like so, thanks! will store my transforms access now. Thanks Eric once again for your great insight :)

avatar image by0log1c · Jul 09, 2011 at 01:08 AM 0
Share

Sorry to bump a resolved question but I just have to ask for confirmation that it also apply to Transform since they're required for any GameObject? I've been storing pretty much every reference except this one. Should I start storing it on Awake?

avatar image Eric5h5 · Jul 09, 2011 at 01:26 AM 0
Share

@BY0LOG1C: Yes...just because it's required doesn't make it an exception; it's still a component.

avatar image
-1

Answer by joseph b · May 10, 2011 at 06:58 PM

transform and gameObject are like links say your on youtube and thers a vidio caled transform when you click wach vidio its like typing.transform if that explanes it thats the simplest way i can put it

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 Owen-Reynolds · May 10, 2011 at 07:01 PM

Most are just pointers (I heard that the rule in C# was classes are implicit pointers, and structs aren't. I'm an old C++ programmer, where pointers are explicit.) Many Unity examples use pointer tricks like:

Transform myBullet = Instantiate(bullet, ... , ... );
Rigidbody RB = myBullet.rigidbody;    // RB is a ref to the rigidbody
bulletScript BS = myBullet.getComponent<bulletScript>(); // BS is a ref to THAT class instance
RB.velocity = Vector3.up;  // these really do change my bullet, not a local copy
BS.lifeSpan = 3;

Many suggestions are to do exactly as you are saying: do all the Find's and GetComponents in Start and save the results.

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

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

2 People are following this question.

avatar image avatar image

Related Questions

Making a GetComponent array 1 Answer

Messenger.Broadcast VS GetComponent 1 Answer

Unity Best Practices - Cached Components 1 Answer

Adding Arbitrary Properties to GameObjects 1 Answer

does using get component slow down game a lot? 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