- Home /
Is "transform.position._" an expensive call?
I mean; does it spend any significant amount of processing time when I look up the transform component of the object the script is on?
Ought I to declare;
xPos = transform.position.x;
yPos = transform.position.y;
zPos = transform.position.z;
at the start of any function where I will be using position information more than twice, and then use the variable xPos instead of writing out transform.position.x?
Or is this really the same thing except for the time it takes to type it out, and assigning variables is a waste for this purpose?
Answer by Eric5h5 · Mar 19, 2011 at 06:03 PM
The best and simplest thing to do is not have separate xPos, yPos, and zPos variables, and just use a Vector3 like transform.position does. Then do "pos = transform.position;
". Things like .transform aren't really "expensive" at this point (it's been optimized compared to what it used to be), but it's still better to cache it in a variable if you're going to use it a lot.
Thanks- I didn't even think of that, but I could check X,Y, and Z components of the saved Vector3 variable as many times as I like and never have to look up the transform more than once in a function. I've got some rewriting to to.
Answer by efge · Mar 19, 2011 at 05:45 PM
Regarding to this video it is an expensive call:
Unite '07 Presentation - Performance Optimization
Your answer
Follow this Question
Related Questions
how to assign a variable to Debug.Log() ? 1 Answer
Optimize my game? 2 Answers
What more should I be caching within my scripts? 3 Answers
Optimizing OnGUI 1 Answer
the variable has not be assigned 4 Answers