not really, since the rotation is going to change. What you’ve done is store the initial rotation of the object.
The point about caching the transform is that it points to a single, non changeable entity which you then don’t have to look up every time you want to interact with it.
Unity doesn’t cache the Transform component. Using the transform shortcut is faster than using GetComponent, but not as fast as caching it. As for the question, no, that’s bad practice unless you need to store the initial rotation for some reason, in which case you should use a descriptive variable name such as “initialStoredRotation”.
No, basically a cached version like “myTransform = transform;” then doing “myTransform.rotation = someVector;” is faster than just using “transform.rotation = someVector;”, which that one would be faster than “GetComponent().rotation = someVector;”.
Those are three separate ways to do the same thing differently
“[2] in Unity5 we also cache the transform component on the c# side, so there should no longer be a performance reason to cache the transform component yourself.”
This is exactly why usually code optimizations are a WASTE OF TIME.
Another optimization which goes around the forums involves eliminating empty LateUpdate() and FixedUpdate() functions. This appears to not be worth the effort also. I should probably focus on other areas instead now.
Thanks.
I know about that, but unfortunately it’s not true. Perhaps they intended to do that, but it didn’t happen in the released version. It’s trivial to test this for yourself…don’t just take someone’s word for it, not even Unity’s.
You should definitely remove empty *Update functions. If they exist, they have overhead (involving reflection) even if there’s nothing in them, plus they make your code ugly. It’s hardly “effort” to remove them.
Not you specifically, why would anyone? You said that removing empty methods is not a code improvement. Regardless of weather it is or not, why would anyone have empty functions in their code?
Take the following for example:
when i started unity a few months ago I had the following in each of my scripts:
currentUpdateType = UPDATE;
public void Update()
{
if (currentUpdateType == UPDATE)
PerformUpdate();
}
pulic void FixedUpdate()
{
if (currentUpdateType == FIXED_UPDATE;
PerformUpdate();
}
public void PerformUpdate()
{
//..do whatever needs to be done
}
This allowed my to just change the currentUpdate variable based on what type of update I wanted to use.
sometimes it was needed to change to a different update type during runtime.
For example, i have a player moving in Update in certain scenes. but then in other scenes, the player is moved in FixedUpdate instead because it is being moved externally by some physics force. Then it can switch back to Update.
But, could not someone consider one of these functions to be “empty” at any given time? If currentUpdate = UPDATE, then technically FIxedUpdate is “empty”. And vice-versa. This is why I wondered if its really worth the “effort” to change something like this just for optimization.
(I was using this same template for every class).
Then i went and deleted all the above updates in every class if they werent actually needed, but did not really notice any performance increase in the profiler.
I can see if someone had hundreds of these, it might impact performance, but i cant image 10 scripts would have that big of an impact.
if (! itIsBroken())
{
dontFixIt();
}
As far as the caching transform, I went ahead now and did that, just to get the most possible optimizations.
Technically it’s not empty at all…an empty function has no code in it. Testing for a condition and then returning if false is not empty by any definition; you’re doing valid work.