Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 mngu021 · Feb 08, 2018 at 01:31 PM · transformupdatingfasterinitialisation

2 Simple Questions. The Questions Are Comments In The Code

 public class Example : MonoBehaviour
     {
         private Transform myTransform;
         private Vector3 myPosition;
 
         private void Start()
         {
             SetInitialReferences();
         }
 
         private void Update()
         {
             Debug.Log(myTransform.position.ToString()); //why does this update constantly?
             Debug.Log(myPosition.ToString()); //this doesent update as I expected not like the other one
         }
 
         void SetInitialReferences()
         {
             myTransform = transform; //why is it faster to use myTransform later on than transform?
             myPosition = transform.position;
         }


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 PizzaPie · Feb 08, 2018 at 02:16 PM 1
Share

myTransform = transform means that the your variable will point to the actual Transform Object because it is a reference type (as all classes). So it doesn't really copy it just points to the same Object in memory. As on if it is faster to cashe the reference rather picking it through the property transform if there is any difference it is trivial.

On the other hand myPosition = transform.position which is of Vector3 type copies the value so afterwards there are 2 different Objects in memory, thus any changes on the transform.position won't be reflected on the myPosition var as it is Value Type.

Search on google about reference types and value types there are alot of sites (including $$anonymous$$SDN official site) to find out more about them.

Cheers.

avatar image mngu021 PizzaPie · Feb 08, 2018 at 11:08 PM 0
Share

Sry for not seeing your answer earlier. Was the first time I used the forums and did not see that you commented as a reply ins$$anonymous$$d of an answer. Anyway thanks both of you!

1 Reply

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

Answer by MUG806 · Feb 08, 2018 at 02:09 PM

If I'm understanding the question, this comes down to the difference between objects and structs.

When you store "transform.position" in myPosition, you actually store a copy of the vector coordinates in the variable, because Vector3 is a struct. When you assign the object "transform" to myTransform, you are actually storing a reference to a transform object, which is just the way C# does things with objects. This way when you access myTransform.position, you are getting the original transform object, and then finding its current position, whereas with myPosition, you are just accessing a copy of the position as it was when you assigned the variable.

Can be tricky to get your head around at first, but C# just handles assigning objects differently to structs and basic data types like int and float.

Comment
Add comment · Show 4 · 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 mngu021 · Feb 08, 2018 at 05:34 PM 0
Share

Thx for the answer! Is clear now :) One last thing... in the method "SetInitialReferences" I set a reference of the transfrom to myTransform. I have heard that it is fast that way if want to access the transform over the reference ins$$anonymous$$d of the actual transfrom. Do you know why that is? And if yes, why is it that way?

avatar image MUG806 · Feb 09, 2018 at 09:25 AM 0
Share

$$anonymous$$ight be mistaken but I don't think that's the case. You may be getting mixed up with GetComponent() which you can use to access components attached to the game object. That method IS slow so the advice is to create a reference to the components you will be needing every frame just the once in initialisation.

avatar image Bonfire-Boy MUG806 · Feb 09, 2018 at 10:34 AM 1
Share

I think you are mistaken. You can get a performance improvement from cacheing it. See eg https://forum.unity.com/threads/cache-transform-really-needed.356875/ It's no longer as slow as a GetComponent call, but still quicker to cache it yourself. Not so much as to make a huge difference, but I$$anonymous$$O it does little harm to get into the habit, at least for Transforms that are being accessed every frame.

avatar image MUG806 Bonfire-Boy · Feb 10, 2018 at 12:34 PM 0
Share

Alright, TIL. I'd submit that as an answer cuz that is definition worth knowing.

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

97 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 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 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 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

Why using = isn´t fast enough for changing Vector3 values from the transform? 2 Answers

Get list of updating transforms 0 Answers

Removing Vector3 Decimal for Position 2 Answers

Move an object toward an angle in 2d space 0 Answers

Trying to rotate child independently of parent... 0 Answers


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