- Home /
Accessing the Transform component from another GameObject is Slower than you imagine!
I am a very professional in unity, I have more than 6 years experience in unity, and I discovered something strange today, It is weird how we learn things when we thought we are professional enough!
I have created a script that should access 2 cameras at the same time (it is an AR app and I use 2 cameras to do scaling without sacrificing in the physics behavior (we cannot simply play with the scales of augmented objects, however, this is another story...). I attach this script to one of the two cameras for more than 1 year without any problem, however I decided to clean up things yesterday, and I attached the same script now to another dedicated GameObject (it is empty) that I named 'Manager' (I hate managers by the way, in Unity, and in Real life :))
and guess what !! I noticed that my Augmented Objects now float slightly on the ground. I wasted more than 2 days to discover why I have this strange behavior, I tested everything. and Finally, I discovered that I should attach the script to one of the 2 cameras !!! my last theory (and I don't know why or if this is true, I should do more testing but I don't have time) is that it is slower if you want to access the Transform component of the camera from another GameObject even if you have a reference to the camera in your script. simply you should access the Transform component from a script attached to the camera itself ...
I think I discovered that because I am dealing with a very sensitive visual effect (an AR object tracked by 2 cameras) Otherwise I would not have noticed this thing with conventional scenarios...
any thoughts?
How do you retrieve the Transform
component? When? How do you move your Augmented Objects?
I pass the camera GameObject as a reference to the other script in the inspector, I just drag and drop it, I don't access it from GetComponent in each frame ...
Referencing the Transform
ins$$anonymous$$d of the GameObject
will be more efficient. Caching yourself the transform of the other object may be even more efficient (I can't remember if the transform
getter is automatically cached by Unity)
I noticed that my Augmented Objects now float slightly on the ground.
I deduced from this that you move the objects, aren't you?
and I don't move my Augmented Objects, it is simply any model (I tested it with many models and always I have the same effect)
I don't know I don't have numbers, but your eyes obviously see the difference. I use a new iPhone with AR$$anonymous$$it (and Vuforia) So I know when the objects float on the ground. I did change nothing in the original script, I just remove it from the camera to a $$anonymous$$anager when I saw this effect, I simply returned it to the camera...
no, I totally don't get it. how is this, ff even noticeable, difference in speed related to floating above the ground?
Your answer
Follow this Question
Related Questions
Using interfaces in C# 4 Answers
Component based architecture Unit, AI, Health, Attack 0 Answers
Unity UI Capability 0 Answers
Physics architecture. When first step after collision happens? 0 Answers
Light bake artifacts 19.3 0 Answers