- Home /
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.
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.
Yea not sure why I put gameObject in there. So storing my own reference will be faster then?
uhm, did not know that .transform was like so, thanks! will store my transforms access now. Thanks Eric once again for your great insight :)
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?
@BY0LOG1C: Yes...just because it's required doesn't make it an exception; it's still a component.
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
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.
Your answer
Follow this Question
Related Questions
Adding Arbitrary Properties to GameObjects 1 Answer
does using get component slow down game a lot? 1 Answer
[SerializeField] for self components instead of GetComponent<>? 1 Answer
When or where do I set a prefab script subclass property? 2 Answers
Monobehaviour shared properties across single gameobject 2 Answers